Laravel Artisan commands cheat sheet

This cheat sheet brings together the most useful Artisan commands for managing a Laravel application on a server: cache and performance, database migrations, queues and the task scheduler, maintenance mode, and development commands. You can search for a command, filter by topic, and copy it with one click.

Information and help

  • php artisan list

    Lists all the Artisan commands available in the application, including commands added by packages.

  • php artisan help migrate

    Shows the help screen of a command: its description, arguments, and all its options. In this example, the help screen of migrate.

  • php artisan --version

    Shows the Laravel version of the application.

  • php artisan about

    Shows an overview of the application: the environment, debug mode, the cache, queue, and session drivers, and which files are cached.

  • php artisan about --only=environment

    Shows one section of the overview, in this example the environment details. The --json option shows the overview in JSON format.

  • php artisan config:show database

    Shows all the values of one configuration file, in this example config/database.php.

  • php artisan env

    Shows the name of the current environment of the application, which is the value of APP_ENV.

The .env file and the encryption key

  • cp .env.example .env

    Creates the .env configuration file from the example file on a new installation. You don't commit .env to the code repository, because it contains passwords and keys, and each server has its own file. You do commit .env.example, with sample values, to the repository.

  • php artisan key:generate

    Generates a new encryption key and writes it to APP_KEY in the .env file. Run it once on a new installation. Caution: in a live application, changing the key signs out all the signed-in users, and data encrypted with the previous key can no longer be decrypted.

  • php artisan key:generate --show

    Only displays a new key, without writing it to the .env file.

  • php artisan env:encrypt

    Encrypts the .env file into a .env.encrypted file, which you can commit to the code repository. The decryption key is shown in the output, and you keep it in a safe place, for example in a password manager.

  • php artisan env:decrypt

    Decrypts .env.encrypted into the .env file. The command takes the key from the LARAVEL_ENV_ENCRYPTION_KEY environment variable or from the --key option.

Cache and performance on a production server

  • php artisan optimize

    Caches the configuration, events, routes, and views in a single command. Run it every time you deploy a new version to the server.

  • php artisan optimize:clear

    Deletes all the cache files that optimize created, and also all the keys in the default cache store.

  • php artisan config:cache

    Combines all the configuration files into a single cache file. After config:cache, the .env file is no longer loaded, and env() returns null for every variable defined only in .env. That's why you call env() only inside the configuration files in the config directory, and use config() in the rest of the code.

  • php artisan config:clear

    Deletes the configuration cache, and the configuration is loaded again from the files and from .env on every request.

  • php artisan route:cache

    Saves all the routes to a cache file, so route registration is faster. After you add or change a route, you need to rebuild the cache, so run it only when you deploy a new version.

  • php artisan route:clear

    Deletes the route cache.

  • php artisan view:cache

    Precompiles all the Blade templates, so they aren't compiled at request time.

  • php artisan view:clear

    Deletes all the compiled templates.

  • php artisan event:cache

    Caches the mapping between the events and the listeners that were discovered automatically.

  • php artisan event:clear

    Deletes the event cache.

  • php artisan cache:clear

    Clears the application cache in the default store. Caution: clearing deletes all the keys in the store, so when several applications share the same cache store, for example the same Redis database, the keys of the other applications are deleted too.

  • php artisan cache:clear redis

    Clears a specific cache store, by its name in the config/cache.php file. In this example, the redis store.

Routes

  • php artisan route:list

    Lists all the routes of the application: the HTTP method, the URI, the name, and the action that handles the request.

  • php artisan route:list --path=api

    Shows only the routes whose URI starts with api.

  • php artisan route:list --except-vendor

    Hides the routes defined by third-party packages. The --only-vendor option shows only those routes.

  • php artisan route:list -v

    Also shows the middleware of each route. The -vv option also lists the middleware inside each group.

Database and migrations

  • php artisan migrate

    Runs all the migrations that haven't run yet.

  • php artisan migrate --force

    On a production server, the migrate command asks for confirmation before it runs, to protect the data. The --force option skips the confirmation, so you use it in a deployment script. Back up the database before you run it on a production server.

  • php artisan migrate:status

    Shows which migrations have already run and which are still pending.

  • php artisan migrate --pretend

    Shows the SQL queries the migrations would run, without actually running them.

  • php artisan migrate --force --isolated

    When several servers run the same deployment, only one server runs the migrations, and the others skip them. Requires a cache store shared by all the servers.

  • php artisan migrate:rollback

    Caution: rolls back the last batch of migrations that ran. Rolling back a migration that created a table or a column deletes it together with the data in it.

  • php artisan migrate:rollback --step=5

    Caution: rolls back the last five migrations.

  • php artisan migrate:fresh

    Danger: drops all the tables in the database, and then runs all the migrations from the beginning. The command also drops tables with a different prefix, so never run it on the database of a live site or on a database shared with other applications.

  • php artisan migrate:fresh --seed

    Danger: like migrate:fresh, and at the end it fills the database with data from the seeders. Suitable for a development environment.

  • php artisan db:seed

    Runs the main seeder, DatabaseSeeder, which fills the database with initial data.

  • php artisan db:seed --class=UserSeeder

    Runs only one seeder. On a production server, the command asks for confirmation, and the --force option skips it.

Files and packages

  • php artisan storage:link

    Creates a symbolic link from public/storage to the storage/app/public directory, so files saved to the public disk are accessible from the browser. Run it once on each server.

  • php artisan vendor:publish --provider="Laravel\Tinker\TinkerServiceProvider"

    Copies the configuration files and templates of a package into the application, so you can change them. In this example, the Tinker configuration file.

  • php artisan vendor:publish --tag=laravel-errors

    Copies files into the application by tag, in this example the Laravel error page templates. Without options, the command shows a list to choose from.

Queues

  • php artisan queue:work

    Starts a worker process that runs the jobs in the queue. The process keeps running until you stop it, so on a production server you run it through a process manager such as Supervisor, which starts it again when it stops.

  • php artisan queue:work --tries=3 --timeout=90

    The --tries option sets how many times to attempt a job before it's recorded as a failed job. The --timeout option sets how many seconds a job can run, and the default is 60. The --timeout value should be a few seconds shorter than the retry_after value in the config/queue.php file, otherwise a job may run twice.

  • php artisan queue:work --queue=high,default

    Processes the jobs in the high queue first, and moves on to the default queue only when high is empty.

  • php artisan queue:work redis

    Processes jobs from a specific queue connection, by its name in the config/queue.php file.

  • php artisan queue:work --max-time=3600

    The worker exits after one hour, and the process manager starts it again, which frees memory that has built up. The --max-jobs=1000 option does the same by number of jobs.

  • php artisan queue:restart

    Tells all the workers to exit gracefully after the current job, so no job is lost. Workers keep the application in memory and don't see new code, so run it every time you deploy a new version, and the process manager starts them again with the new code. The command stores the signal in the cache, so a configured cache store is required.

  • php artisan queue:failed

    Lists the failed jobs: ID, connection, queue, and failure time.

  • php artisan queue:retry ce7bb17c-cdd8-41f0-a8ec-7b4fef4e5ece

    Pushes a failed job back onto the queue by its ID. You can give several IDs, or retry all the jobs of one queue with --queue.

  • php artisan queue:retry all

    Pushes all the failed jobs back onto the queue.

  • php artisan queue:forget ce7bb17c-cdd8-41f0-a8ec-7b4fef4e5ece

    Deletes one job from the list of failed jobs.

  • php artisan queue:flush

    Caution: deletes all the failed jobs, regardless of their age. With the --hours=48 option, only jobs that failed 48 hours ago or earlier are deleted.

  • php artisan queue:prune-failed --hours=48

    Deletes the records of jobs that failed more than 48 hours ago. Without --hours, records older than 24 hours are deleted.

  • php artisan queue:clear

    Caution: deletes all the jobs waiting in the default queue, before they run. The php artisan queue:clear redis --queue=emails command deletes jobs only from a specific queue. Works with Redis, database, and SQS queues.

The task scheduler

  • * * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1

    The only cron entry the scheduler needs: it runs schedule:run every minute, and Laravel decides which tasks to run. Replace the path with your project directory, and add the line with crontab -e as the user that runs the application.

  • php artisan schedule:run

    Checks which scheduled tasks are due now and runs them. It usually runs from cron, not manually.

  • php artisan schedule:list

    Lists all the scheduled tasks and when each one runs next.

  • php artisan schedule:work

    Runs the scheduler in the foreground, every minute, until you stop it. Suitable for a local development environment, instead of a cron entry.

  • php artisan schedule:interrupt

    Stops a schedule:run invocation that's still running, when tasks are scheduled to run several times a minute. Run it at the end of a deployment, so the scheduler doesn't keep running the previous code until the end of the minute.

Maintenance mode

  • php artisan down

    Puts the application into maintenance mode: every request gets a maintenance page with a 503 status code. In maintenance mode, queued jobs and scheduled tasks don't run either.

  • php artisan down --secret="1630542a-246b-4b66-afa1-dd72a4c43515"

    Sets a bypass token. Visiting the site URL with the token, for example https://example.com/1630542a-246b-4b66-afa1-dd72a4c43515, sets a cookie in the browser and lets you browse the site normally during maintenance. Use only letters, digits, and hyphens in the token.

  • php artisan down --with-secret

    Generates a random bypass token and shows it in the output.

  • php artisan down --render="errors::503"

    Shows a prerendered maintenance page, even before the application loads. This way, visitors don't run into errors while composer install updates the packages.

  • php artisan down --retry=60

    Sends the Retry-After header with a value of 60 seconds along with the maintenance page. The --refresh=15 option makes the browser refresh the page automatically after 15 seconds.

  • php artisan down --redirect=/

    Redirects all requests during maintenance to a single URL, in this example the home page.

  • php artisan up

    Takes the application out of maintenance mode.

Restarting services

  • php artisan reload

    Ends the long-running services, such as the queue workers, the scheduler, and Laravel Octane, in a single command, and the process manager starts them again with the new code. Run it at the end of a deployment.

  • php artisan octane:reload

    Available in an application that has the separate Laravel Octane package installed. Gracefully restarts the Octane workers, so the new code is loaded into memory. Octane keeps the application in memory, so run it after every deployment.

  • php artisan octane:status

    Shows whether the Octane server is running. It's also available only when the Laravel Octane package is installed in the application.

Development and generating files

  • php artisan tinker

    Opens an interactive console (REPL) with full access to the application: models, jobs, and events. Caution: on a production server, every command runs on the real data, and changes or deletions take effect immediately.

  • php artisan make:controller UserController

    Creates a new controller in the app/Http/Controllers directory.

  • php artisan make:controller PhotoController --resource

    Creates a controller with ready-made actions for displaying, creating, updating, and deleting (resource controller).

  • php artisan make:model Flight -m

    Creates an Eloquent model in the app/Models directory, and the -m option also creates a migration for its table.

  • php artisan make:migration create_flights_table

    Creates a new migration file in the database/migrations directory. From the name create_flights_table, Laravel detects that the migration creates the flights table.

  • php artisan make:command SendEmails

    Creates a new Artisan command in the app/Console/Commands directory.

The recommended order for deploying a new version to the server: update the code, run composer install --no-dev --optimize-autoloader, and then php artisan migrate --force, php artisan optimize, and finally php artisan queue:restart, so the queue workers load the new code. In an application that runs with Laravel Octane, add php artisan octane:reload. The php artisan reload command ends the queue workers, the scheduler, and other services all at once, and the process manager starts them again. The Composer commands page explains how to install packages on a production server.

After config:cache, the .env file isn't loaded, so a change in .env takes effect only after you run php artisan config:cache or php artisan optimize again. For the same reason, calling env() outside the configuration files returns null. On a production server, set APP_ENV=production and APP_DEBUG=false in the .env file, because an active debug mode may expose sensitive configuration details to visitors.

Run Artisan commands from the project directory, as the user that runs the application and not as root, so the files created in the storage and bootstrap/cache directories keep the right permissions. Add the scheduler's cron entry to the crontab of that same user as well.

Did you know?

On a Laravel VPS, the server comes with FrankenPHP and Laravel Octane, along with PHP, Composer, Redis, and Supervisor, preinstalled and configured, with full root access over SSH, so you can run the Artisan commands on this page right after you upload your application. On a Linux VPS, you connect over SSH with full root access, install PHP and Composer, and run every command on this page.

Laravel VPS plans

Laravel Artisan commands FAQ

What's the right order of Artisan commands when I deploy to a server?

After you update the code, run composer install --no-dev --optimize-autoloader, and then php artisan migrate --force to update the database, php artisan optimize to cache the configuration, routes, and views, and finally php artisan queue:restart, so the queue workers load the new code. In an application that runs with Laravel Octane, add php artisan octane:reload.

Why doesn't a change in the .env file take effect?

When the configuration is cached, after config:cache or optimize, the application doesn't read the .env file. That's why you run php artisan config:cache or php artisan optimize again after every change in .env, and the new values take effect on the next request. The queue workers and Laravel Octane, which keep the application in memory, pick them up only after a restart, for example with php artisan reload.

Why do the queue workers keep running the old code?

A worker started with queue:work loads the application once and keeps it in memory, so it doesn't see changes in the code. The php artisan queue:restart command tells all the workers to exit gracefully after the current job, and the process manager, such as Supervisor, starts them again with the new code.

How do I run the Laravel task scheduler on a server?

Add one line to the crontab of the user that runs the application, which runs php artisan schedule:run every minute: * * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1. The schedules themselves are defined in the application code, and the php artisan schedule:list command lists them along with the next run time of each one.

How can I work on the site during maintenance mode?

Put the application into maintenance mode with a bypass token, for example php artisan down --secret="1630542a-246b-4b66-afa1-dd72a4c43515", and visit the site URL with the token at the end. The browser gets a cookie, and from then on the site appears normally only to you, while all the other visitors see the maintenance page. When you finish, run php artisan up.

Shopping cart

Your cart is emptyYou can start with one of our popular plans.

Site search

Accessibility

Text size
100%
Accessibility statement