Laravel interview question · Most asked
What is mass assignment, and how do $fillable and $guarded protect against it?
Summary
Mass assignment is creating or updating a model from an array, such as User::create($request->all()). Without protection, a client can set columns it should never control, like is_admin or balance.
Longer Answer
$fillable is an allow-list of attributes that may be mass assigned; $guarded is a deny-list ($guarded = [] disables protection entirely). Laravel silently drops attributes that are not fillable (or throws if you enable Model::preventSilentlyDiscardingAttributes()).
Model-level protection is the second line of defence. The first is to never pass raw input: validate with a Form Request and pass $request->validated() (or $request->safe()->only([...])), so only fields you validated reach the model.
Prefer $fillable over $guarded: a new sensitive column added later is protected by default.
Code Example
class Order extends Model
{
protected $fillable = ['shipping_address', 'notes'];
}
public function update(UpdateOrderRequest $request, Order $order)
{
$this->authorize('update', $order);
$order->update($request->validated());
}
Key Points
- Mass assignment = filling a model from an array.
$fillable(allow-list) is safer than$guarded.- Pass
validated()data, never$request->all().
More most-asked Laravel questions
- Gates or policies: how does authorization work in Laravel?
- How do queues work in Laravel?
- How do database transactions work in Laravel, and what pitfalls should you know?
- How do you validate requests in Laravel? Why use Form Requests?
- Which Artisan commands would you run when deploying Laravel to production, and why?
- What is the service container, and how does dependency injection work in Laravel?
- What is the N+1 query problem in Eloquent and how do you prevent it?
- A Laravel endpoint is slow. How do you find and fix the cause?