本文介紹了Laravel 5:Model.php 中的 MassAssignmentException的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我收到此錯誤:
Model.php 第 448 行中的 MassAssignmentException:_token
MassAssignmentException in Model.php line 448: _token
當我使用 create
方法時.請查看以下代碼:
When I am using create
method. Please review code below:
Contacts.php(模型):
class Contacts extends Model
{
protected $table = ['name', 'mobile', 'email', 'address', 'created_at', 'updated_at'];
}
ContactsController.php(控制器):
public function store(Request $request)
{
$inputs = $request->all();
$contacts = Contacts::Create($inputs);
return redirect()->route('contacts.index');
}
推薦答案
對于 Mass Assignment例外:您應該通過對屬性$fillable
的創(chuàng)建或更新操作來指定您想要批量分配的模型的所有字段:
For the Mass Assignment Exception: you should specify all the fields of the model that you want to be mass-assignable through create or update operations on the property $fillable
:
protected $fillable = ['name', 'mobile', 'email', 'address', 'created_at', 'updated_at'];
此外,$table
字段應該只包含模型的表名:
Besides, the field $table
should contain only the model's table name:
protected $table = 'your_table_name';
這篇關于Laravel 5:Model.php 中的 MassAssignmentException的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!