Artisan is the name of the command-line interface included with Laravel. It provides a number of helpful commands for your use while developing your application. It is driven by the powerful Symfony Console component.
To view a list of all available Artisan commands, you may use the list
command:
php artisan list
Every command also includes a "help" screen which displays and describes the command's available arguments and options. To view a help screen, simply precede the name of the command with help
:
php artisan help migrate
You may specify the configuration environment that should be used while running a command using the --env
switch:
php artisan migrate --env=local
You may also view the current version of your Laravel installation using the --version
option:
php artisan --version
Sometimes you may wish to execute an Artisan command outside of the CLI. For example, you may wish to fire an Artisan command from an HTTP route. Just use the Artisan
facade:
Route::get('/foo', function()
{
$exitCode = Artisan::call('command:name', ['--option' => 'foo']);
//
});
You may even queue Artisan commands so they are processed in the background by your queue workers:
Route::get('/foo', function()
{
Artisan::queue('command:name', ['--option' => 'foo']);
//
});
In the past, developers have generated a Cron entry for each console command they wished to schedule. However, this is a headache. Your console schedule is no longer in source control, and you must SSH into your server to add the Cron entries. Let's make our lives easier. The Laravel command scheduler allows you to fluently and expressively define your command schedule within Laravel itself, and only a single Cron entry is needed on your server.
Your command schedule is stored in the app/Console/Kernel.php
file. Within this class you will see a schedule
method. To help you get started, a simple example is included with the method. You are free to add as many scheduled jobs as you wish to the Schedule
object. The only Cron entry you need to add to your server is this:
* * * * * php /path/to/artisan schedule:run 1>> /dev/null 2>&1
This Cron will call the Laravel command scheduler every minute. Then, Laravel evaluates your scheduled jobs and runs the jobs that are due. It couldn't be easier!
Let's look at a few more scheduling examples:
$schedule->call(function()
{
// Do some task...
})->hourly();
$schedule->exec('composer self-update')->daily();
$schedule->command('foo')->cron('* * * * *');
$schedule->command('foo')->everyFiveMinutes();
$schedule->command('foo')->everyTenMinutes();
$schedule->command('foo')->everyThirtyMinutes();
$schedule->command('foo')->daily();
$schedule->command('foo')->dailyAt('15:00');
$schedule->command('foo')->twiceDaily();
$schedule->command('foo')->weekdays();
$schedule->command('foo')->weekly();
// Schedule weekly job for specific day (0-6) and time...
$schedule->command('foo')->weeklyOn(1, '8:00');
$schedule->command('foo')->monthly();
$schedule->command('foo')->mondays();
$schedule->command('foo')->tuesdays();
$schedule->command('foo')->wednesdays();
$schedule->command('foo')->thursdays();
$schedule->command('foo')->fridays();
$schedule->command('foo')->saturdays();
$schedule->command('foo')->sundays();
$schedule->command('foo')->monthly()->environments('production');
$schedule->command('foo')->monthly()->evenInMaintenanceMode();
$schedule->command('foo')->monthly()->when(function()
{
return true;
});
$schedule->command('foo')->sendOutputTo($filePath)->emailOutputTo('foo@example.com');
Note: You must send the output to a file before it can be mailed.
$schedule->command('foo')->sendOutputTo($filePath);
$schedule->command('foo')->thenPing($url);