5

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.

Nirmalz Thapaz
  • 925
  • 4
  • 13
  • 28

5 Answers5

10

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();
Gadoma
  • 6,475
  • 1
  • 31
  • 34
3

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

Amit Garg
  • 3,867
  • 1
  • 27
  • 37
2

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

class Author extends Eloquent {

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

}

Laurence
  • 58,936
  • 21
  • 171
  • 212
1

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');
fideloper
  • 12,213
  • 1
  • 41
  • 38
  • still the same error, MassAssignMentException. By the way I read the documentation on Mass assignment. But it didnt help me a lot. sorry to be a pain but what do this mean: 'mass-assignable' ... – Nirmalz Thapaz Sep 09 '13 at 14:45
  • that means that the attributes you list in $fillable are massassign-enabled = you can autopopulate them with that mechanism. – Gadoma Sep 09 '13 at 14:49
  • You can't use `create()` for `fill()` methods without defining which fields are "mass-assignable" in the "fillable" array. Conversely, there's also a [guarded array](https://github.com/laravel/framework/blob/4.0/src/Illuminate/Database/Eloquent/Model.php#L119). – fideloper Sep 09 '13 at 15:02
  • Now it's working, it was a silly mistake from my side. I added this property in controller which was a blunder, when added to model, it worked, thanks – Nirmalz Thapaz Sep 10 '13 at 08:43
0

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.

Khuram
  • 1,820
  • 1
  • 26
  • 33