Workaround for laravel mass assignment exception

Saibal Roy • April 18, 2020

quick-tips laravel

Hello Everyone!

Welcome to my website and this is Saibal with yet another post to share my knowledge with you all.

A lot of beginners face this mass assignment exception on the Eloquent model.

There are couple of workarounds to this mass assignment exception:

First, we can set the \$fillable property of a model and be explicit with the attributes that are mass assignable.

protected $fillable = ['name'];

Second, we can set the \$guarded property of a model with the attributes that aren't mass assignable or we can set it to an empty array to make all attributes mass assignable.

protected $guarded = ['price'];

or,

protected $guarded = [];

Third, if you want it disable application wide and you know what you are doing then go the app\Providers\AppServiceProvider.php.

Import at the top:

use Illuminate\Database\Eloquent\Model;

and then go the boot method and write:

Model::unguard();

So, these are the possible workarounds for the laravel mass assignment exception.

Happy learning folks!