Mass assignment protection helps keep your app safe by controlling which data can be saved all at once. It stops unwanted or harmful data from being added to your database.
Mass assignment protection in Laravel
class User extends Model { protected $fillable = ['name', 'email', 'password']; // or protected $guarded = ['is_admin']; }
$fillable lists fields allowed for mass assignment.
$guarded lists fields NOT allowed for mass assignment.
title and content can be mass assigned.class Post extends Model { protected $fillable = ['title', 'content']; }
price can be mass assigned.class Product extends Model { protected $guarded = ['price']; }
is_admin is guarded, it will NOT be set by mass assignment.User::create(['name' => 'Anna', 'email' => 'anna@example.com', 'is_admin' => true]);
This example shows a User model with name and email allowed for mass assignment. The is_admin field is not fillable, so it will be ignored when creating the user.
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class User extends Model { protected $fillable = ['name', 'email']; } // In a controller or tinker $userData = ['name' => 'John', 'email' => 'john@example.com', 'is_admin' => true]; $user = User::create($userData); // Output user attributes print_r($user->toArray());
Always use either $fillable or $guarded to protect your models.
Setting $guarded = [] means all fields are mass assignable (not recommended).
Mass assignment protection helps prevent security issues like users changing admin status.
Mass assignment protection controls which fields can be set in bulk.
Use $fillable to allow specific fields or $guarded to block specific fields.
This keeps your app safe from unwanted data changes.
Practice
Solution
Step 1: Understand mass assignment concept
Mass assignment means setting multiple model attributes at once, often from user input.Step 2: Identify protection purpose
Mass assignment protection controls which attributes can be set this way to prevent unwanted changes.Final Answer:
To control which model attributes can be set in bulk -> Option BQuick Check:
Mass assignment protection = control allowed fields [OK]
- Confusing mass assignment with password encryption
- Thinking it speeds up queries
- Believing it creates migrations automatically
Solution
Step 1: Recall correct property for allowed fields
In Laravel,$fillablelists fields allowed for mass assignment.Step 2: Check syntax and visibility
The property should beprotectedand named$fillablewith an array of allowed fields.Final Answer:
protected $fillable = ['name', 'email']; -> Option CQuick Check:
$fillable = allowed fields [OK]
- Using $guarded to allow fields instead of block
- Making $fillable public instead of protected
- Confusing $fillable and $guarded names
User::create(['name' => 'Anna', 'email' => 'anna@example.com', 'role' => 'admin']);?
class User extends Model {
protected $fillable = ['name', 'email'];
}Solution
Step 1: Check $fillable fields
The model allows mass assignment only for 'name' and 'email'.Step 2: Understand behavior for unfillable fields
The 'role' field is ignored during mass assignment, no error is thrown.Final Answer:
User is created with name and email set; role is ignored -> Option DQuick Check:
Only $fillable fields set, others ignored [OK]
- Expecting an error for unfillable fields
- Thinking all fields are set regardless of $fillable
- Assuming unfillable fields become null
class Post extends Model {
protected $guarded = [];
}
You want to prevent mass assignment on the 'is_admin' field. What is the best fix?Solution
Step 1: Understand current $guarded setting
Currently, $guarded is empty array, so all fields are mass assignable.Step 2: Block 'is_admin' field
To block a field, add it to $guarded array to prevent mass assignment.Final Answer:
Add 'is_admin' to the $guarded array -> Option AQuick Check:
$guarded blocks fields; add 'is_admin' there [OK]
- Adding blocked fields to $fillable instead
- Removing $guarded and expecting protection
- Blocking all fields unintentionally with ['*']
Solution
Step 1: Understand the goal
Allow all fields except 'password' and 'is_admin' for mass assignment.Step 2: Choose correct property
Using$guardedto block specific fields is best for this case.Step 3: Evaluate options
Settingprotected $guarded = ['password', 'is_admin'];blocks those fields and allows others.Final Answer:
protected $guarded = ['password', 'is_admin']; -> Option AQuick Check:
$guarded blocks fields; use it to exclude only some [OK]
- Using $fillable with only blocked fields (wrong meaning)
- Setting $fillable to ['*'] which is invalid
- Leaving $guarded empty and expecting protection
Ready to prove it?
Pass the test and this pattern moves unknown
