Eloquent and Data
6. What is the N+1 query problem, and how do you prevent it in Laravel?
N+1 happens when you load N parent models in one query and then lazy-load a relation for each one, which adds N more queries. Eager loading fixes it: Post::with('author')->get() runs two queries no matter how many posts there are.
Two settings help catch it. Model::preventLazyLoading(! app()->isProduction()) throws an exception on any lazy load outside production.
Since Laravel 12.8, Model::automaticallyEagerLoadRelationships() can also eager-load a relation for the whole collection the first time one model touches it. See Eloquent relationships.
7. How do polymorphic relationships work, and what is a morph map?
A polymorphic relation lets one model belong to several parent types through two columns. A Comment can belong to a Post or a Video using commentable_id and commentable_type.
The child uses morphTo(); each parent uses morphMany() or morphOne().
By default the type column stores the full class name, such as App\Models\Post. Renaming the class then breaks every stored row.
Relation::enforceMorphMap(['post' => Post::class]) stores a short alias instead, and it throws if a model has no alias. A senior candidate sets it up on day one.
8. What is the difference between a global scope and a local scope?
A global scope adds a constraint to every query on a model automatically. A local scope is a named constraint you apply by hand.
SoftDeletes is the best-known global scope: it adds whereNull('deleted_at') to every query. Local scopes read like query methods, as in User::popular()->active()->get().
The current docs define them with a #[Scope] attribute on a protected method; older codebases use the scopePopular() naming style. Global scopes surprise people, so remove one explicitly with withoutGlobalScope() when you need all rows.
9. How do you write accessors and mutators in current Laravel?
Define one protected method named after the attribute that returns an Attribute object. The old getFirstNameAttribute() and setFirstNameAttribute() pair is the legacy style.
protected function firstName(): Attribute
{
return Attribute::make(
get: fn (string $value) => ucfirst($value),
set: fn (string $value) => strtolower($value),
);
}
For type conversion, casts are simpler: 'is_admin' => 'boolean', 'options' => 'array', or an enum class. Password hashing is usually the 'hashed' cast rather than a hand-written mutator.
10. What is mass assignment, and how does Laravel guard against it?
Mass assignment is filling a model from an array, such as User::create($request->all()). Without a guard, an attacker could add is_admin=1 to the form and set a column you never meant to expose.
Laravel only fills attributes listed in $fillable (or not listed in $guarded). Unlisted keys are dropped silently, which can hide bugs.
Model::preventSilentlyDiscardingAttributes() makes them throw instead, which is worth enabling outside production. The safest habit is to pass $request->validated(), never $request->all().
11. How do you use database transactions, and what goes wrong with jobs inside them?
DB::transaction(fn () => ...) commits if the closure returns and rolls back if it throws. A second argument, attempts: 5, retries the closure when the database reports a deadlock.
The trap is dispatching a job or event inside the transaction. A fast worker can pick up the job before the commit, then fail to find the row.
Mark the job with afterCommit(), or implement ShouldDispatchAfterCommit on the event, so it waits for the commit and is dropped on rollback.
12. What are API resources for?
API resources turn models into JSON in a controlled way, so the response shape does not depend on the table's columns. A UserResource lists exactly which fields leave the server.
$this->whenLoaded('posts') includes a relation only if it was eager loaded, which prevents a resource from causing N+1 queries. $this->when($user->isAdmin(), ...) adds fields conditionally.
Laravel 13 also added first-party JSON:API resources for teams that must follow that specification.