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:cache merges all config files into one cached file. After this, env() returns null outside config files, so only call env() inside config/*.php.
  • route:cache, event:cache and view:cache remove per-request route registration, listener discovery and Blade compilation. php artisan optimize runs these together.
  • migrate --force runs migrations non-interactively. Migrations must be backward compatible with the code still serving traffic during a rolling deploy.
  • queue:restart tells 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:restart after 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

← All Laravel questions