0
0
Laravelframework~20 mins

Mass assignment protection in Laravel - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Mass Assignment Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when mass assignment is used without protection?

Consider a Laravel model with no $fillable or $guarded properties defined. What will happen if you try to create a new model instance using mass assignment with request data?

Laravel
User::create($request->all());
AOnly attributes defined in the database schema will be saved, others ignored silently.
BLaravel will throw a MassAssignmentException error preventing the creation.
CThe model will be created with all attributes from the request, potentially including sensitive fields.
DThe model will be created but all attributes will be set to null.
Attempts:
2 left
💡 Hint

Think about what happens if you don't specify which fields are allowed for mass assignment.

📝 Syntax
intermediate
2:00remaining
Which code correctly protects against mass assignment?

Given a Laravel model, which of the following correctly protects the model from mass assignment vulnerabilities?

Laravel
class Post extends Model {
    // protection here
}
Aprotected $guarded = [];
Bprotected $guarded = ['title', 'content'];
Cprotected $fillable = ['*'];
Dprotected $fillable = ['title', 'content'];
Attempts:
2 left
💡 Hint

Remember, $fillable defines allowed fields, $guarded defines disallowed fields.

🔧 Debug
advanced
2:00remaining
Why does this mass assignment fail with MassAssignmentException?

Given this model and controller code, why does the mass assignment fail?

Laravel
class Product extends Model {
    protected $guarded = ['id'];
}

// In controller
Product::create(['id' => 5, 'name' => 'Book', 'price' => 10]);
ABecause 'id' is guarded, but it is included in the create array, causing the exception.
BBecause 'name' and 'price' are not in $fillable, mass assignment is blocked.
CBecause $guarded should be empty to allow mass assignment.
DBecause the model is missing a $fillable property, mass assignment always fails.
Attempts:
2 left
💡 Hint

Check which fields are guarded and what is being assigned.

state_output
advanced
2:00remaining
What is the value of $user->admin after mass assignment?

Given this User model and code, what will be the value of $user->admin after creation?

Laravel
class User extends Model {
    protected $fillable = ['name', 'email'];
}

$user = User::create(['name' => 'Alice', 'email' => 'alice@example.com', 'admin' => true]);
Atrue
Bfalse
Cnull
DThrows MassAssignmentException
Attempts:
2 left
💡 Hint

Check which fields are allowed in $fillable and what happens to others.

🧠 Conceptual
expert
2:00remaining
Why is mass assignment protection important in Laravel?

Which of the following best explains why Laravel uses mass assignment protection?

ATo prevent users from setting sensitive model attributes they should not control, avoiding security risks.
BTo improve database query performance by limiting fields.
CTo automatically validate user input before saving to the database.
DTo allow mass assignment only for admin users.
Attempts:
2 left
💡 Hint

Think about what could happen if users could set any attribute freely.