Home » Php » Unable to create a model with Eloquent create method. Error telling MassAssignMentException

Unable to create a model with Eloquent create method. Error telling MassAssignMentException

Posted by: admin November 29, 2017 Leave a comment

Questions:

I have created a model named, Author. I tried creating a model with the help of eloquent create method like this:

public function postCreate(){
   Author::create(array(
       'user' => Input::get('user'),
       'info' => Input::get('info') 
   ));
   return Redirect::to('authors')
        ->with('message', 'User created successfully');
}

‘user’ and ‘info’ are the name of the form elements. I am sure I am not mistaken with typo. When I run this, models is not created and says MassAssignmentException.

But when I tried with the following method, the model is created and was saved in the table

public function postCreate(){

    $author = new Author;
    $author->name = Input::get('user');
    $author->info= Input::get('info');
    $author->save();

    return Redirect::to('authors')
        ->with('message', 'User created successfully');

}

And I really want to use the create method, its just looks much cleaner and simpler.

Answers:

this should work for you:

1) as already listed by @fideloper and @the-shift-exchange, in your Author model you need to create the below field (this is a white-list of all database columns you want to be available for autopopulation [mass assignment] )

 protected $fillable = array('user','info', ... ,'someotherfield'); 

2) use the below code to fire the mass asssignment mechanism

$author = new Author;
$author->fill(Input::all());
$author->save();

Questions:
Answers:

You need to set the Mass Assignment fields. In your Author model:

class Author extends Eloquent {

protected $fillable = array('name', 'bio');

}

Questions:
Answers:

I was getting the MassAssignmentException when I have extends my model like this.

class Author extends Eloquent {

}

I was trying to insert array like this

Author::create($array);//$array was data to insert.

Issue has been resolve when I created Author Model as given below.

class Author extends Eloquent {
    protected $guarded = array();  // Important
}

Reference https://github.com/aidkit/aidkit/issues/2#issuecomment-21055670

Questions:
Answers:

Your model needs to have the $fillable variable set.

See the documentation on mass-assignment for details.

It will look something like this in your Author model:

protected $fillable = array('user', 'info');

Questions:
Answers:

You need to use protected $fillable property by assigning it an array of fields/columns which you want to fill/assign values to. For example you have a model with fields f1, f2, f3 and f4. You want to assign values to f1, f2 and f3 but not to f4 then you need to use:

protected $fillable = ['f1', 'f2', 'f3'];

The above line would allow to pass an array to the:

$mod = Model::create($arr);
$mod->save();

Whatever the $arr array contains, but only f1, f2, and f3 will be assigned values (if values exists in the $arr array for f1, f2, f3).

Hope it will help you and others.