Interview Q&A · questions with model answers
Laravel Interview Questions
Ordered by how often they're asked. Most-asked answers are free to read in full; members see every answer and can rate their confidence on each.
-
Most asked How do database transactions work in Laravel, and what pitfalls should you know?
DB::transaction(fn () => ...) commits if the closure returns and rolls back if it throws. It accepts a retry count for deadlocks. Side effects such as dispatching jobs or sending mail should wait…
-
Most asked How do queues work in Laravel?
A job class implementing ShouldQueue is serialised and pushed to a queue backend (Redis, database, SQS, Beanstalkd). Worker processes (php artisan queue:work) pop jobs and run their handle method,…
-
Most asked Walk me through the lifecycle of an HTTP request in Laravel.
The request enters public/index.php, the application is bootstrapped, the HTTP kernel runs it through global middleware, the router matches a route and runs route middleware, the controller returns…
-
Most asked How do facades work, and when would you prefer dependency injection instead?
A facade is a class with static-looking methods that forwards each call to an object resolved from the service container. Cache::get() is really app('cache')->get().
-
Most asked How does middleware work in Laravel, and where is it registered in recent versions?
Middleware wraps the request: each one receives the request and a $next closure, can act before and after calling $next, or return a response early. In Laravel 11 and later it is configured in…
-
Most asked Explain Eloquent relationship types, including polymorphic relationships.
One-to-one (hasOne/belongsTo), one-to-many (hasMany/belongsTo), many-to-many (belongsToMany with a pivot table), has-one/has-many-through, and polymorphic relationships where a model can belong to…
-
Most asked Explain $tries, $backoff, $timeout and retry_after for Laravel jobs. How do they interact?
$tries is how many attempts a job gets, $backoff how long to wait between attempts, $timeout how long one attempt may run before the worker kills it, and the connection's retry_after how long the…
-
How do events, listeners and observers work, and when would you use them?
Events decouple 'something happened' from the reactions: you dispatch OrderPlaced, and any number of listeners (synchronous or queued) respond. Observers group Eloquent model lifecycle hooks…
-
How do accessors, mutators and attribute casting work in modern Eloquent?
Casts convert attributes between database and PHP types automatically ('paid_at' => 'datetime', 'options' => 'array', enums, encrypted values). Accessors and mutators are defined with a method…
-
How do soft deletes work, and what are their downsides?
The SoftDeletes trait sets deleted_at instead of deleting the row, and a global scope hides those rows from queries. withTrashed(), onlyTrashed(), restore() and forceDelete() manage them.
-
How does Laravel protect against CSRF, and when do you exclude routes?
The web middleware group verifies a CSRF token on every state-changing request (POST, PUT, PATCH, DELETE): forms include @csrf, and JavaScript sends the X-CSRF-TOKEN or X-XSRF-TOKEN header.
-
How does task scheduling work in Laravel, and how do you run it safely on several servers?
One cron entry runs php artisan schedule:run every minute; the schedule defined in code (routes/console.php in recent versions) decides which tasks are due. On multiple servers, use onOneServer() so…
-
How do sessions work in Laravel when you run several web servers?
Sessions must live in a shared store (Redis, database, Memcached, or the encrypted cookie driver) instead of the default local file driver; otherwise users are logged out when the load balancer…
-
paginate, simplePaginate or cursorPaginate: what is the difference?
paginate() runs a count query and supports page numbers; simplePaginate() skips the count and only knows next/previous; cursorPaginate() uses a 'where id > last seen' cursor, which stays fast on…
-
How do you handle file uploads and storage in Laravel?
Use the Storage facade (Flysystem) with disks configured in config/filesystems.php: local, public, s3 and others. Validate uploads, store them with generated names, and serve private files through…
-
How does Blade escaping work, and what are components and slots?
{{ $value }} escapes HTML with htmlspecialchars; {!! $value !!} outputs raw HTML. Components (<x-alert type="error">) are reusable view pieces backed by a class or anonymous Blade file, with named…
-
Where should business logic live in a Laravel app? Service classes, actions or repositories?
Keep controllers thin: put business logic in plain classes such as services or single-purpose action classes (PlaceOrder), injected by the container. Repositories over Eloquent are usually…
-
How do you handle and report exceptions in Laravel?
Exceptions go through the exception handler, configured in bootstrap/app.php (->withExceptions(...)) in Laravel 11+, where you can report them (logs, Sentry), customise rendering (render), ignore…
-
How does broadcasting work in Laravel (Reverb, Pusher, Echo)?
Events implementing ShouldBroadcast are pushed through a queue to a WebSocket server (Laravel Reverb, Pusher, Ably), and the front end listens with Laravel Echo. Private and presence channels are…
-
How does Laravel's Pipeline work, and where does the framework use it?
Pipeline sends an object through a list of 'pipes' (classes or closures), each receiving the object and a $next callback. HTTP middleware is built on it, and you can use it for your own multi-step…
-
How does maintenance mode work, and how do you use it safely?
php artisan down makes the app return 503 for everyone, optionally with a secret bypass URL (--secret), a pre-rendered page (--render), and a Retry-After header; php artisan up brings it back.