Laravel is one of the most popular PHP frameworks out there, and one of the most commonly deployed frameworks on Launchdeck. Check out our Laravel deployment quickstart page if you'd like to get your Laravel app up and running in minutes!
For a more comprehensive tutorial on deploying Laravel apps, check out our Laravel deployment guide on how to deploy Laravel.
If you've been working with the Laravel framework for a while, you will likely have run into the "Artisan" command-line interface. As the official command-line interface bundled with Laravel, Artisan is a pretty ingenious part of the framework. It runs entirely in PHP and is hence invoked as
php artisan and offers the full spectrum of CLI goodness you might be familiar with from other interfaces - interactive sessions, output formatting, progress bars, you name it. When you run
php artisan <some-command>, your entire application as well as the vendor libraries are loaded up just like a regular HTTP request to your application.
Let's look at some of the ways to use various Artisan commands in your development and deployment workflows, as well as some related tips and tricks.
First off, let's examine how we can get an overview of the available commands within a specific project. Note that the exact list of commands depends entirely on the version of Laravel you're running, which packages you have installed, and which (if any) custom commands are defined - more on that later! To do so, you may run either
php artisan list or just
$ php artisan list ... Available commands: clear-compiled Remove the compiled class file db Start a new database CLI session down Put the application into maintenance / demo mode ... etc
To find out which commands exist within a specific namespace, use
php artisan list <namespace>
Looking for something more specific? Try grep! (if you're on a POSIX system)
$ php artisan | grep rollback migrate:rollback Rollback the last database migration
The data in your database changes over time and so does the structure of most tables. When a migration needs to occur in the process of deploying a new version, correct timing is of the essence: run the migrations too early and a column to which some controller in an old version of your application is still referring might have been deleted. Run them too late and some new logic in your latest version might be trying to access a table that is yet to be created.
This is why you want to run your migrations as soon as your new version goes live, and preferably enable maintenance mode for it as well. Here's an example of how to set this up using the SSH commands feature in Launchdeck, as commands in the "After publish" section.
Note the use of the
--force flag. This is because by default, Laravel interactively prompts for confirmation when trying to run the migrations in a production environment (
APP_ENV) and the SSH commands don't run in an interactive shell.
Not everyone prefers to have their migrations run automatically, and it's not necessarily the right strategy for every single database schema modification, but it's the most reliable method in the vast majority of cases.
You might also want to clear all caches (this includes the application cache, bootstrap files, and config, views and route caches) when a new version goes live. Simply add another "After publish" command with
php artisan optimize:clear in order to do so.
Note that this only makes sense when you're using a cache driver such as redis. If you're using the filesystem cache, you might as well have it write to somewhere within the release and make sure those paths are not persistent ('shared').
Tips & tricks
Are you (still) on a POSIX system? Add an alias to your
alias pa='php artisan'
pa migrate the next time you've added a new migration!
This one's really powerful when trying to figure out some intricacy of the framework, or just experimenting in general.
php artisan tinker (or
pa tinker after that last alias) is essentially a PHP "repl" but with the entire context of your application loaded and ready to go. Try something like:
pa tinker User::first();
Artisan also allows you to define custom commands. They can be great tools for system management (such as adding new users in an early phase of your new project), for long running operations, and for general miscellaneous "housekeeping" routines as well.
For more info, check out the official Artisan docs.