Laravel 5 Modify Mass Assignment

328 views Asked by At

How I can change the fillable attribute of a model on the fly?

For example, I have User model with, protected $fillable = ['name', 'email', 'password']

When updating the user, I want to exclude 'email' from mass assignment so that the email is not changed on update.

1

There are 1 answers

0
Emeka Mbah On BEST ANSWER

Mass assignment doesn't mean all the field listed in fillable will be auto filled.

You still have control over what to save in the table.

So if you do:

$user = User::find(1);
$user->email = '[email protected]';
$user->save();

Only email will be saved in the example above while name and password remains the same