Question

Why the first code work, but the second code doesn't work?

First:

$user = new User;
$user->name = Input::get('name');
$user->email = Input::get('email');
$user->password = Hash::make( Input::get('password') );
$user->save();

Second:

User::create(array(
    'name' => Input::get('name'),
    'email' => Input::get('email'),
    'password' => Hash::make( Input::get('password') ),
));

Error message:

Illuminate \ Database \ Eloquent \ MassAssignmentException
name
Was it helpful?

Solution

You need to set the $fillable variable on the model to let it know which fields can be Mass Assigned.

Laravel Mass Assignment

Update:

as deczo and har2vey have stated you can also use $guarded as a blacklist instead of $fillable as a whitelist.

OTHER TIPS

For a simpler solution especially if you have no fields to guard, use $guarded instead:

//On top of your model class
protected $guarded = array();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top