What are Eloquent Accessors and Mutators?
Why Interviewers Ask This
This question tests conceptual clarity. Interviewers want to hear a precise, confident definition before moving to more complex Laravel topics. It also reveals how well you can explain technical ideas to non-experts.
Answer
Accessors and Mutators allow you to transform Eloquent attribute values when reading and writing. In Laravel 9+ (using the newer syntax), define them as a method returning an Attribute: protected function firstName(): Attribute { return Attribute::make(get: fn($value) => ucfirst($value), set: fn($value) => strtolower($value)); }. Access: $user->first_name automatically applies the getter. Assign: $user->first_name = "ALICE" stores "alice". Accessors can compute values from multiple fields: fn() => $this->first_name . " " . $this->last_name. Cast attributes to types in the $casts property: "is_admin" => "boolean", "options" => "array", "created_at" => "datetime".
Pro Tip
Before answering, structure your response: one-line definition → real-world analogy → concrete example from a project. This makes even complex Laravel answers easy to follow.