DEV Community

Cover image for Laravel Interview Questions With Answers From Real Projects
Muhammad Ali Mughal
Muhammad Ali Mughal

Posted on

Laravel Interview Questions With Answers From Real Projects

By Muhammad Ali Mughal · Last updated 24 October 2026

Most lists of Laravel interview questions test whether you've read the docs: "What is Artisan?", "What is a migration?". Interviewers who've shipped Laravel apps ask something else. They want to hear what broke, how you found it and what you changed. The best way to prepare is to answer each question with a mechanism and a real example, and to be ready to show it in code.

I'm Muhammad Ali Mughal, a principal software engineer with 9+ years of PHP/Laravel and .NET work. At Loop Origin I've built Laravel apps for e-commerce, logistics and SaaS clients,. Below are 15 questions I'd ask a mid-level or senior Laravel developer, with short answers that show real experience, plus a small test I ran to back one of them up.

An open laptop with blurred code on a meeting room table, next to a glass of water and a notebook with a pen

Laravel interview questions on Eloquent and the database

1. What is the N+1 query problem, and how do you fix it?

Looping over models and touching a relationship runs one query for the list plus one per row. Fix it with eager loading: Order::with('customer')->get(). A strong answer adds how you catch it: Model::preventLazyLoading() in non-production environments, plus a query log or Debugbar during review.

I checked this on PHP 8.4 with illuminate/database v13.38.0 and an in-memory SQLite database seeded with 50 customers and 200 orders, counting queries with DB::listen:

$queries = 0;
DB::connection()->listen(function () use (&$queries) { $queries++; });

foreach (Order::all() as $o) { $o->customer->name; }              // lazy
foreach (Order::with('customer')->get() as $o) { $o->customer->name; } // eager
Customer::withCount('orders')->withSum('orders', 'total')->get();   // aggregates
Order::chunkById(50, fn ($chunk) => $chunk->count());               // batches
Enter fullscreen mode Exit fullscreen mode

The output (with the counter reset before each line):

Lazy loading, 200 orders:      201 queries
Eager loading with('customer'): 2 queries
withCount + withSum, 50 customers: 1 queries (first: 4 orders, sum 6004)
preventLazyLoading: Illuminate\Database\LazyLoadingViolationException
chunkById(50) over 200 orders: 5 queries
Enter fullscreen mode Exit fullscreen mode

201 queries down to 2. That's the kind of evidence I like to hear in an interview.

2. When would you use chunkById() instead of get()?

When processing thousands of rows in a job or command. get() loads everything into memory. chunkById() pages by primary key, so rows updated during the loop aren't skipped or repeated, which can happen with offset-based chunk(). For read-only exports, lazyById() gives the same safety with a simpler loop.

3. How do you prevent two requests from overselling stock?

Wrap it in DB::transaction() and either lock the row with lockForUpdate() or use an atomic conditional update: Product::whereKey($id)->where('stock', '>', 0)->decrement('stock'), then check the affected row count. Mention that validation alone can't solve a race.

4. $fillable or $guarded?

Both protect against mass assignment. I prefer $fillable on models that take user input, because new columns stay protected by default. The deeper answer: validate with Form Requests and pass $request->validated(), never $request->all().

5. How do you write a migration that's safe on a large production table?

Avoid long table locks. Add nullable columns first, backfill in batches through a job, then add constraints. Never edit a migration that has already run in production; write a new one. Senior candidates also mention testing down(), or deciding consciously not to support it.

Laravel interview questions on queues and performance

6. A queued job runs twice. Why, and what do you do?

Usually retry_after is shorter than the job's runtime, so the job is released while still running, or a worker crashed after the side effect but before acknowledging it. Fix the timing, then make the job idempotent: check state before acting, or use unique jobs. I covered the full setup in Running Laravel Queues in Production.

7. Why doesn't my deployed fix take effect in queued jobs?

Queue workers are long-running processes holding the old code in memory. Run php artisan queue:restart (or horizon:terminate) on every deploy. It's a small question, but it tells me whether someone has run Laravel in production.

8. How do you rate-limit calls to a third-party API from jobs?

Use job middleware such as RateLimited or ThrottlesExceptions, together with $tries and an increasing $backoff. For SMS gateways and courier APIs I also log every request and response for support.

9. What do you cache, and how do you invalidate it?

Cache expensive, read-heavy data such as settings, menus and report totals with Cache::remember(). Invalidate on write through model events or explicit Cache::forget() calls. Good candidates mention cache tags on Redis, stampede protection with Cache::lock(), and running config:cache and route:cache on deploy.

Laravel interview questions on architecture and security

10. How does the service container help you?

It resolves dependencies through constructor injection and lets you bind interfaces to implementations. A typical example: binding a PaymentGateway interface to a JazzCash or Stripe class per environment, and swapping in a fake in tests.

11. Middleware, policies or gates for authorisation?

Middleware handles request-level checks such as authentication. Policies hold model-level rules ("can this user update this order?"), gates hold ad-hoc abilities. For larger role systems I've used Spatie Permission with policies, so controllers only call $this->authorize().

12. Where should business logic live?

Not in controllers. I use action or service classes with a single public method, called from controllers, jobs and commands alike. That keeps logic testable and reusable. Strong candidates explain the trade-off instead of reciting a pattern name.

13. How do you protect a Laravel app against common attacks?

Eloquent and the query builder use parameter binding against SQL injection, Blade's {{ }} escapes output against XSS, and CSRF tokens are on by default for web forms. The mistakes come from DB::raw() with user input, {!! !!} on user content and missing authorisation checks on IDs.

Testing and debugging questions

14. How do you test code that sends emails or calls APIs?

Use fakes: Mail::fake(), Queue::fake(), Event::fake() and Http::fake() with Http::preventStrayRequests(). Then assert what was sent. Feature tests with RefreshDatabase cover the full request cycle.

15. A page got slow in production. Walk me through it.

Reproduce with real data, check query count and duration (Telescope, Debugbar or slow query log), look for N+1s and missing indexes with EXPLAIN, then cache or queue what's left. The order matters more than the tools.

How to prepare: a quick checklist

Area What to have ready
Eloquent One N+1 story with before and after query counts
Queues A duplicate-job or stuck-worker story and the fix
Data integrity How you handled a race condition
Architecture Where you put business logic and why
Testing Which fakes you use and one bug a test caught
Your CV Projects you can explain line by line

If your applications aren't reaching the interview stage yet, my guide on why resumes get rejected by ATS covers the step before this one.

FAQ

What Laravel interview questions are asked for experienced developers?

Expect questions on N+1 queries and eager loading, queue reliability and idempotency, race conditions and transactions, caching and invalidation, authorisation with policies, and testing with fakes. Senior rounds add architecture trade-offs and production debugging stories, so prepare concrete examples with numbers rather than definitions alone.

How should I prepare for a Laravel technical interview?

Rebuild two or three features you've shipped as small demos you can explain line by line. Practise answering each question with the problem, the mechanism and what you'd change next time. Read the current Laravel docs for queues, Eloquent relationships and testing, because version details often come up.

What are the most common Laravel performance questions?

The usual ones are N+1 queries, missing database indexes, loading too many rows with get(), caching expensive queries, queueing slow work like emails and PDFs, and production commands such as config:cache, route:cache and OPcache. Be ready to explain how you measured the problem before fixing it.

Do Laravel interviews include live coding?

Many do, usually a small task such as a CRUD endpoint with validation, a relationship query or fixing a failing test. Some companies send a take-home project instead. In both cases, clear structure, validation, tests and a short README tend to matter more than finishing every optional feature.

Should I learn Livewire or Inertia before a Laravel interview?

Learn the one the job ad mentions. Livewire suits teams that want server-rendered interactivity in Blade, while Inertia suits teams using Vue or React. Knowing the core framework well matters more than either, but being able to discuss why a team might choose one is a plus.

Hiring a Laravel team instead?

If you'd rather hire a team that has already answered these questions in production, see our Laravel development services, or visit Loop Origin.

Which Laravel interview question caught you off guard? Share it in the comments.

laravel #php #interview #career #webdev #backend

Top comments (0)