3

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 Answers1

1

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@emails.com';
$user->save();

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

Emeka Mbah
  • 16,745
  • 10
  • 77
  • 96