Performance: Mass assignment protection
This concept affects server-side processing speed and security, indirectly impacting page load by preventing unnecessary database writes and errors.
User::create($request->only(['name', 'email']));
User::create($request->all());
| Pattern | Database Operations | Validation Cost | Security Risk | Verdict |
|---|---|---|---|---|
| Assign all input fields | High - all fields saved | High - many validations | High - risk of unwanted data changes | [X] Bad |
| Assign only allowed fields | Low - minimal fields saved | Low - fewer validations | Low - controlled data changes | [OK] Good |
$fillable lists fields allowed for mass assignment.protected and named $fillable with an array of allowed fields.User::create(['name' => 'Anna', 'email' => 'anna@example.com', 'role' => 'admin']);?
class User extends Model {
protected $fillable = ['name', 'email'];
}class Post extends Model {
protected $guarded = [];
}
You want to prevent mass assignment on the 'is_admin' field. What is the best fix?$guarded to block specific fields is best for this case.protected $guarded = ['password', 'is_admin']; blocks those fields and allows others.Pass the test and this pattern moves unknown