Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mralston's avatar

$fillable and $guarded precedence

What is the precedence / order in which $fillable and $guarded are evaluated?

I believe that setting $guarded to an empty array allows mass assignment of all properties, but does that override $fillable or does $fillable take precedence?

I'm reviewing some old code and have found a few models on which $fillable has been populated and $guarded set to an empty array.

My intention is to remove the $guarded = [] code as it seems dangerous to me, but I'm wondering how this will affect the current functionality. I could of course add every attribute to $fillable - this would negate any logic changes of removing $guarded = [], but obviously leaves the same security issue that I see currently.

0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

The order in which $fillable and $guarded are evaluated is as follows:

  1. If $guarded is set to an empty array ($guarded = []), it allows mass assignment of all properties, regardless of the $fillable array.
  2. If $guarded is not set to an empty array, then the $fillable array is evaluated. Only the attributes listed in the $fillable array are allowed for mass assignment.

In your case, if you remove the $guarded = [] code, it will default to the Laravel's default behavior, which is to guard against mass assignment. This means that you will need to explicitly define the attributes that are allowed for mass assignment in the $fillable array.

To maintain the current functionality and remove the security issue, you can add every attribute to the $fillable array. This way, only the attributes listed in the $fillable array will be allowed for mass assignment.

Here's an example of how you can define the $fillable array in your model:

protected $fillable = ['attribute1', 'attribute2', 'attribute3'];

Replace 'attribute1', 'attribute2', 'attribute3' with the actual attributes you want to allow for mass assignment.

Remember to always validate user input before mass assigning it to your models to ensure data integrity and security.

1 like

Please or to participate in this conversation.