Laravel 5 : MassAssignmentException in Model.php
Asked Answered
U

7

25

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');
}
Uriniferous answered 2/1, 2016 at 11:45 Comment(1)
MassAssignmentException in LaravelLeboff
J
90

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'];

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

protected $table = 'your_table_name';
Janey answered 2/1, 2016 at 11:48 Comment(0)
H
6

This might happen in case if you have used the wrongly imported the class. if you are using the User Model.

Wrong Import

// mostly IDE suggestion
use Illuminate\Foundation\Auth\User;

Correct Model Import

use App\User;

i have gone through this. might help someone.

Humbertohumble answered 29/7, 2016 at 7:43 Comment(1)
Yes it did (: Auto import in IDE may confuse sometimesColan
L
5

You can all column fillable:

protected $guarded = array();

Add your model.

Laccolith answered 26/4, 2017 at 10:16 Comment(0)
L
1

You need only to add the following to your Model (Contact):

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

For example:

class Contacts extends Model { 
   protected $table = ['name', 'mobile', 'email', 'address', 'created_at', 'updated_at']; 
   protected $fillable = [ 'name', 'mobile', 'email', 'address', 'created_at', 'updated_at' ]; 
}
Lionhearted answered 3/4, 2019 at 22:28 Comment(4)
Can you provide a but more detail. Are you suggesting that they don't need the store function, that your provided $fillable should replace $table, or something else?Australoid
Mr Sandeep want to insert data in the contact table so he need to go in the Contact Model and Put protected $fillable = [ 'name', 'mobile', 'email', 'address', 'created_at', 'updated_at' ];Lionhearted
So you suggest that this should be added to the his Contact class, in addition to the code he already provided?Australoid
Yes Exactly ! like This class Contacts extends Model { protected $table = ['name', 'mobile', 'email', 'address', 'created_at', 'updated_at']; protected $fillable = [ 'name', 'mobile', 'email', 'address', 'created_at', 'updated_at' ]; }Lionhearted
F
0

If all of the above fails, you can try following.

Put following after namespace.

use Eloquent;

Put following at the start of your store method.

Eloquent::unguard();

like:

public function store(Request $request)
{        
   Eloquent::unguard();
   $inputs = $request->all();
   $contacts = Contacts::Create($inputs);
   return redirect()->route('contacts.index');
}

This is not recommended though, as this makes things vulnerable to attacks. But if you need a quick fix this might help.

Frail answered 13/10, 2017 at 12:5 Comment(0)
S
0

Check Model you have Imported or Not. If not then use this.

<?php 

 namespace App\Http\Controllers\Auth; 
 use App\Http\Controllers\Controller; 
 use App\User;
Swamy answered 10/1, 2018 at 11:44 Comment(0)
K
0

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.

Kurd answered 3/10, 2018 at 20:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.