Model::unguard() does temporarily disable the mass assignment protection of the model, so you can create records without worrying about the fillable. It’s not recommended to it for the users, but only for specific task such as seeding etc.
We have a
category table in our application.
And in the model
protected $fillable = [ 'name', ]
When you try the below code, it will insert the category status as false, even though we have set the status as true. In another way, it will not consider the status field as an entry in the array as it’s not in the fillable array.
Category::create([ 'name' => "Sports", 'status' => true ])
To overcome this we can use like this
Model::unguard(); Category::create([ 'name' => "Sports", 'status' => true ]) Model::reguard();
This will temporarily disable the amazing security feature, and reenable it after our execution.