Save/updating 个问题 Laravel
Save/updating problems in Laravel
我一直在试验 Laravel - 我 运行 遇到了 saving/updating 一条记录(用户描述)的问题。我的代码基于一些有效的代码 - 所以我对我搞砸了什么感到有点困惑。
我可以使用 php tinker 进行更改(它反映在页面上 - 所以它不是数据库的东西) - 但是当使用网站本身时,laravel 没有保存在表单中输入的任何文本。
这些不是完整的文件 - 只是我认为可能相关的文件
谢谢! :)
P.S - 它悄无声息地失败了,因此感到沮丧,并且需要堆栈溢出 post :)
用于更改描述的表单元素
{!! Form::model($user, ['method' => 'PATCH', 'action' => ['UserController@update', $user->id]]) !!}
{!! Form::text('description', null, ['class' => 'form-control']) !!}
{!! Form::submit('save', ['class' => 'btn btn-success']) !!}
{!! Form::close() !!}
用户控制器
public function store(EditUserRequest $request)
{
$user = new User($request->all());
Auth::user()->users()->save($user);
return redirect('home');
}
public function edit(User $user)
{
// if owner is posts creator (collection id matches user id)
if ($user->id == Auth::user()->id) {
return view('home');
} else {
return redirect('home');
}
}
public function update(User $user, EditUserRequest $request)
{
// if owner is posts creator (collection id matches user id)
if ($user->id == Auth::user()->id) {
$user->update($request->all());
return redirect('home');
} else {
return redirect('home');
}
}
EditRequestController
<?php
namespace App\Http\Requests;
use App\Http\Requests\Request;
class EditUserRequest extends Request
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'description' => 'required',
];
}
}
dd($user)
User {#142 ▼
#fillable: array:3 [▼
0 => "name"
1 => "email"
2 => "password"
]
#hidden: array:2 [▼
0 => "password"
1 => "remember_token"
]
#connection: null
#table: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:9 [▼
"id" => 1
"name" => "Oliver Williams"
"email" => "Oliver.jack.williams@gmail.com"
"description" => "lorem ipsum nonsense"
"password" => "y$c65lDNJ0V8ibYT4QD6cnk.HmXPwBqlskHWzOzKi1vOlNKVR48y0iS"
"remember_token" => "1e6Il0T1rjQQMKSAn09Gd8FLdh0CcYTs4Xd7B6IYUp02rNJ9IpSpV374kwRg"
"created_at" => "2016-02-17 23:37:39"
"updated_at" => "2016-02-18 00:48:44"
"time" => "0000-00-00 00:00:00"
]
#original: array:9 [▼
"id" => 1
"name" => "Oliver Williams"
"email" => "Oliver.jack.williams@gmail.com"
"description" => "lorem ipsum nonsense"
"password" => "y$c65lDNJ0V8ibYT4QD6cnk.HmXPwBqlskHWzOzKi1vOlNKVR48y0iS"
"remember_token" => "1e6Il0T1rjQQMKSAn09Gd8FLdh0CcYTs4Xd7B6IYUp02rNJ9IpSpV374kwRg"
"created_at" => "2016-02-17 23:37:39"
"updated_at" => "2016-02-18 00:48:44"
"time" => "0000-00-00 00:00:00"
]
#relations: []
#visible: []
#appends: []
#guarded: array:1 [▼
0 => "*"
]
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
}
dd($user->update($request->all()));
true
这不适用于所有人 - 所以我不确定它是否符合答案的条件 - 但我没有将新行添加到 'User.php' 中的 'fillable' 数组 -这意味着它不能被批量分配。
我是个笨蛋。
谢谢大家:)
我一直在试验 Laravel - 我 运行 遇到了 saving/updating 一条记录(用户描述)的问题。我的代码基于一些有效的代码 - 所以我对我搞砸了什么感到有点困惑。
我可以使用 php tinker 进行更改(它反映在页面上 - 所以它不是数据库的东西) - 但是当使用网站本身时,laravel 没有保存在表单中输入的任何文本。
这些不是完整的文件 - 只是我认为可能相关的文件
谢谢! :)
P.S - 它悄无声息地失败了,因此感到沮丧,并且需要堆栈溢出 post :)
用于更改描述的表单元素
{!! Form::model($user, ['method' => 'PATCH', 'action' => ['UserController@update', $user->id]]) !!}
{!! Form::text('description', null, ['class' => 'form-control']) !!}
{!! Form::submit('save', ['class' => 'btn btn-success']) !!}
{!! Form::close() !!}
用户控制器
public function store(EditUserRequest $request)
{
$user = new User($request->all());
Auth::user()->users()->save($user);
return redirect('home');
}
public function edit(User $user)
{
// if owner is posts creator (collection id matches user id)
if ($user->id == Auth::user()->id) {
return view('home');
} else {
return redirect('home');
}
}
public function update(User $user, EditUserRequest $request)
{
// if owner is posts creator (collection id matches user id)
if ($user->id == Auth::user()->id) {
$user->update($request->all());
return redirect('home');
} else {
return redirect('home');
}
}
EditRequestController
<?php
namespace App\Http\Requests;
use App\Http\Requests\Request;
class EditUserRequest extends Request
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'description' => 'required',
];
}
}
dd($user)
User {#142 ▼
#fillable: array:3 [▼
0 => "name"
1 => "email"
2 => "password"
]
#hidden: array:2 [▼
0 => "password"
1 => "remember_token"
]
#connection: null
#table: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:9 [▼
"id" => 1
"name" => "Oliver Williams"
"email" => "Oliver.jack.williams@gmail.com"
"description" => "lorem ipsum nonsense"
"password" => "y$c65lDNJ0V8ibYT4QD6cnk.HmXPwBqlskHWzOzKi1vOlNKVR48y0iS"
"remember_token" => "1e6Il0T1rjQQMKSAn09Gd8FLdh0CcYTs4Xd7B6IYUp02rNJ9IpSpV374kwRg"
"created_at" => "2016-02-17 23:37:39"
"updated_at" => "2016-02-18 00:48:44"
"time" => "0000-00-00 00:00:00"
]
#original: array:9 [▼
"id" => 1
"name" => "Oliver Williams"
"email" => "Oliver.jack.williams@gmail.com"
"description" => "lorem ipsum nonsense"
"password" => "y$c65lDNJ0V8ibYT4QD6cnk.HmXPwBqlskHWzOzKi1vOlNKVR48y0iS"
"remember_token" => "1e6Il0T1rjQQMKSAn09Gd8FLdh0CcYTs4Xd7B6IYUp02rNJ9IpSpV374kwRg"
"created_at" => "2016-02-17 23:37:39"
"updated_at" => "2016-02-18 00:48:44"
"time" => "0000-00-00 00:00:00"
]
#relations: []
#visible: []
#appends: []
#guarded: array:1 [▼
0 => "*"
]
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
}
dd($user->update($request->all()));
true
这不适用于所有人 - 所以我不确定它是否符合答案的条件 - 但我没有将新行添加到 'User.php' 中的 'fillable' 数组 -这意味着它不能被批量分配。
我是个笨蛋。 谢谢大家:)