Sandeep Sandeep - 7 months ago 27
PHP Question

Laravel 5 : MassAssignmentException in Model.php

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');
}

Answer

For the MassAssignmentException: you should specify all the fields of the model that you want to be mass-assignable on the propery $fillable:

protected $fillable =  ['name', 'mobile', 'email', 'address', 'created_at', 'updated_at'];

Besides, the field $table should contain only the model's table name:

protected $table = 'your_table_name';
Comments