I am getting this error:
MassAssignmentException in Model.php line 448: _token
When I am using create
method. Please review code below:
Contacts.php (Model):
class Contacts extends Model
{
protected $table = ['name', 'mobile', 'email', 'address', 'created_at', 'updated_at'];
}
ContactsController.php (Controller):
public function store(Request $request)
{
$inputs = $request->all();
$contacts = Contacts::Create($inputs);
return redirect()->route('contacts.index');
}
Check Model you have Imported or Not. If not then use this.
You can all column fillable:
Add your model.
If all of the above fails, you can try following.
Put following after namespace.
Put following at the start of your
store
method.like:
This is not recommended though, as this makes things vulnerable to attacks. But if you need a quick fix this might help.
This might happen in case if you have used the wrongly imported the class. if you are using the User Model.
Wrong Import
Correct Model Import
i have gone through this. might help someone.
Make sure you are putting the $fillable or $guarded in the app\Contacts.php file and not the app\Http\Controllers\ContactsController.php file. It should be obvious, but it can be overlooked.
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
:Besides, the field
$table
should contain only the model's table name: