Laravel interview question · Most asked
Which Artisan commands would you run when deploying Laravel to production, and why?
Summary
Cache the configuration, routes, events and views (php artisan optimize), run migrations with --force, and restart queue workers with queue:restart so they load the new code.
Longer Answer
config:cachemerges all config files into one cached file. After this,env()returns null outside config files, so only callenv()insideconfig/*.php.route:cache,event:cacheandview:cacheremove per-request route registration, listener discovery and Blade compilation.php artisan optimizeruns these together.migrate --forceruns migrations non-interactively. Migrations must be backward compatible with the code still serving traffic during a rolling deploy.queue:restarttells workers to exit gracefully after their current job; the process supervisor restarts them on the new code. Workers otherwise keep running old code in memory.- Composer install with
--no-dev --optimize-autoloader.
With zero-downtime deploys (a new release directory and an atomic symlink switch), also reload PHP-FPM or OPcache so the old cached bytecode is not served.
Key Points
php artisan optimize(config, routes, events, views).- Never call
env()outside config once config is cached. queue:restartafter every deploy.- Migrations must be compatible with the old code.
Explanation
What interviewers look for
Practical deployment experience: the env() and config cache trap and stale queue workers are the two details that separate people who have shipped Laravel from those who have not.
Follow-up questions
- Why do queue workers keep running old code?
- How do you rename a column without downtime?
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?
- 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?
- What is mass assignment, and how do $fillable and $guarded protect against it?
- A Laravel endpoint is slow. How do you find and fix the cause?