Workaround for laravel mass assignment exception
Saibal Roy • August 16, 2021
quick-tips laravelA lot of beginners face this mass assignment exception on the Eloquent model.
There are couple of workarounds to this laravel 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!