falak hamid
falak hamid

Reputation: 21

MassAssignmentException in laravel5

i used laravel5...and want to store data in database and when press the submit than this error appeared

> MassAssignmentException in C:\xampp\htdocs\marriage\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php line 417: _token

my controller code

public function store(Request $request)
{
    $custom =Request::all();
    Custom::create($custom);

    return redirect('custom');

}

model code

class custom_table extends Model
{
  protected $fillable=[
    'skin_color',
    'cast',
    'residence',
    'family_members',
    'hieght',
    'created_at',
    'updated_at'
];

}

please help me how to resolve this error

Upvotes: 2

Views: 4630

Answers (2)

tread
tread

Reputation: 11088

You need to tell laravel what fields can be updated by the form, otherwise all the data in the form will be used to update the record even _token and _method

In your model:

class Cost extends Model
{
  /**
   * The attributes that are mass assignable.
   *
   * @var array
   */
  protected $fillable = ['canbe', 'updated', 'byform];
}

Upvotes: 0

davsp
davsp

Reputation: 2179

You are running into that error, because your Request is passing a "_token" attribute, and it is not Mass assignable.

You can exclude it using the except method, as such:

public function store(Request $request)
{
    $custom = Request::except('_token'); // Exclude _token attribute
    Custom::create($custom);

    return redirect('custom');

}

Upvotes: 2

Related Questions