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

LaraBABA's avatar

$guarded and $fillable

Hello,

From what I understand from the doc, $fillable is whitelist of elements in an array while $guarded is the black list.

Where I am confused is how could you submit the user id to the database without passing it through a form in laravel? In my old coding without framework, I would have passed the user id of the post via a session and pass it back to a variable for further logic.

ie, form with

name

surname

email

in the database

user_id

name

surname

email

Thank you.

0 likes
4 replies
Cronix's avatar

They both are to tell what is allowed to be mass assignable, and what isn't, but in opposite ways.

$fillable = properties that can be mass assignable. Anything not in the array cannot be mass assigned. $guarded = properties that cannot be mass assignable, allowing all others through.

The manual says to use one or the other, but not both.

So if your db table had the following fields: name, age and location

And you only wanted name and age to be mass assignable, you could use

$fillable = ['name', 'age']; // name and age can be mass assignable, but location can't be

// or

$guarded = ['location']; //location can't be mass assignable, but name and age can

While $fillable serves as a "white list" of attributes that should be mass assignable, you may also choose to use $guarded. The $guarded property should contain an array of attributes that you do not want to be mass assignable. All other attributes not in the array will be mass assignable. So, $guarded functions like a "black list". Of course, you should use either $fillable or $guarded - not both.

https://laravel.com/docs/5.5/eloquent#mass-assignment

4 likes
sutherland's avatar
Level 28

Even if a property is guarded, you can pass it through a form, you just can't mass assign it to the model.

If you have a user_id field, you'd do something like

$user = User::find($request->user_id);

$user->fill([
    'name' => $request->name,
    'surname' => $request->surname,
    'email' => $request->email
]);

$user->save();

But I'd personally use route model binding, where you make the request to a route defined like /users/{user} and then have a controller method like so:

public function update(Request $request, User $user)
{
    // Do whatever you need to update $user
}

Of course I should point out that if you are doing this for the currently authenticated user, there's no reason to pass an ID at all, just use Auth::user().

1 like
hemantadsl's avatar

now i have understood diff between guard and fill

Please or to participate in this conversation.