【发布时间】:2022-02-23 16:39:13
【问题描述】:
如果我更新我的表单模型绑定,我会收到上述错误。 我已经将我所有的数据库字段都放到了可填充字段中,没有任何影响,我仍然收到错误。
这是我的编辑视图的形式
{!! Form::model($tutorial, ['route' => ['tutorials.update', $tutorial->id], 'method' => 'PUT' ]) !!}
我的模型中的可填充/受保护字段
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable =
[
'title',
'subtitle',
'content',
'meta_desc',
'meta_title',
'seo_title',
'tags',
'slug',
'updated_at',
'added_on'
];
protected $guarded = ['id', '_token'];
这是我的 TutorialController
public function update(Tutorial $tutorial)
{
$input = Input::except('_method'); // Request::all() is not working
$tutorial->update($input);
return Redirect::route('tutorials.index')->withSuccess(
'success.'
);
}
最后但并非最不重要的是我的路线
Route::get('edit/{id}', [
'as' => 'tutorials.edit',
'uses' => 'TutorialsController@edit'
]);
Route::put('edit/{id}', [
'as' => 'tutorials.update',
'uses' => 'TutorialsController@update'
]);
【问题讨论】:
-
如果你将方法设置为 PUT Laravel 会自动添加一个隐藏的 _method 字段。它与可填写字段有关,但我不知道为什么/在哪里
-
点击此链接查看 put 方法。laravel.com/docs/4.2/html#opening-a-form 我认为您需要使用小写的 put
-
我知道如何创建一个 put 表单 - 即使我使用 POST 我仍然收到错误。
-
使用此受保护的 $fillable = array('*'); 使所有字段都可以填写然后删除受保护的可填充和保护。然后再试一次