Hello developers,
All of the Laravel developers use Artisan
commands in their Laravel projects. But many of them don't know all the Artisan commands and the hidden features. In this demonstration, we will cover the Artisan Commands in Laravel.
Artisan is the name of the command-line interface included with Laravel. It provides several helpful commands for your use while developing your application. (Source) Artisan exists at the root of your application as the artisan script and provides several helpful commands. To get all the commands you can simply type the below command in the terminal of your project:
php artisan list
Every command includes a help screen to provide help to the developers. The help command is given below:
php artisan help migrate
Writing Commands
Besides the artisan commands, you can write your custom commands. The custom commands are stored in the app/console/Commands directory. To make a new command you should use the make:command
Artisan Command. Initially, the directory does not exist in your application. The first time you need to execute the following command to make the directory by the following command:
php artisan make:command SendEmails
Specifying The Configuration Environment
You can specify the environment while running any command. If the environment should be local, you can write the command listed below:
php artisan migrate --env=local
Displaying Your Current Laravel Version
To display your current Laravel version, you can type:
php artisan --version
Calling Commands Outsite of CLI
Sometimes you need to execute a command outside of CLI, Suppose you want to run a command in Cpanel by your application in the HTTP route. Use the artisan facade and the below example:
Route::get('/example', function()
{
$exitCode = Artisan::call('command:name', ['--option' => 'example']);
});
You may even use a queue to process the command in the background:
Route::get('/example', function()
{
Artisan::queue('command:name', ['--option' => 'example']);
});
Scheduling Artisan Commands
In the previous, developers have generated Cron entries for each console command they wish to schedule. To make things easier, Laravel introduces the Laravel Schedule. The command schedule is stored in app/Console/Kernel.php
file. You can add as many scheduled jobs as you like. 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. More example is listed below:
Scheduling Clouser
To run the command hourly you can write the below command:
$schedule->call(function()
{
// Do some task...
})->hourly();
Scheduling Terminal Commands
To run commands in the terminal you can write the following code:
$schedule->exec('composer self-update')->daily();
Manual Cron Expression
To write a manual Cron in your application, you can use the following code:
$schedule->command('example')->cron('* * * * *');
Frequent Jobs
To use frequent jobs in your applications you can use:
$schedule->command('example')->everyFiveMinutes();
$schedule->command('example')->everyTenMinutes();
$schedule->command('example')->everyThirtyMinutes();
Daily Jobs
For executing the daily jobs in your appliaction, you can use:
$schedule->command('example')->daily();
Daily Jobs At Specific Time (24 hour time)
To run a daily job in a specific time, you need to add the time like below:
$schedule->command('example')->dailyAt('15:00');
Twice Daily Jobs
For executing twice daily jobs in your appliaction, you can use twiceDaily()
by following:
$schedule->command('example')->twiceDaily();
Jobs that run every weekday
To run a job in the weekday, you can use weekdays()
like below:
$schedule->command('example')->weekdays();
Weekly Jobs
For executing jobs that run only weekly basis in your appliaction, you can use weekly()
by following:
$schedule->command('example')->weekly();
// Schedule weekly job for specific day (0-6) and time...
$schedule->command('example')->weeklyOn(1, '8:00');
Monthly Jobs
To run a job in monthly basis, you can use monthly()
like below:
$schedule->command('foo')->monthly();
Jobs That Run On Specific Days
To run a job in the specific days, you can use dayName()
like below:
$schedule->command('example')->mondays();
$schedule->command('example')->tuesdays();
$schedule->command('example')->wednesdays();
$schedule->command('example')->thursdays();
$schedule->command('example')->fridays();
$schedule->command('example')->saturdays();
$schedule->command('example')->sundays();
Prevent Jobs From Overlapping
Generally, the scheduled jobs will be run if the previous job is still running. To prevent this, you can use withoutOverlapping()
like below:
$schedule->command('example')->withoutOverlapping();
Limit The Environment The Jobs Should Run In
To make the specific environment for running any command you should use thye following way:
$schedule->command('example')->monthly()->environments('production');
Indicate The Job Should Run Even When Application Is In Maintenance Mode
To run any command even if the application is in the maintenance mode run the following command:
$schedule->command('example')->monthly()->evenInMaintenanceMode();
Allow Job To Run When Callback Is True
To run any command when the callback is true, simply follow the below command:
$schedule->command('example')->monthly()->when(function()
{
return true;
});
E-Mail the output Of A Scheduled Job
To get e-mail after a job done, use below code. Note that the output file should be present in a directory.
$schedule->command('example')->sendOutputTo($filePath)->emailOutputTo('example@example.com');
Send The Output Of The Scheduled Job To A Given Location
To send the output of the scheduled job to a given location, you can send the file from a specific location like below:
$schedule->command('example')->sendOutputTo($filePath);
Ping A Given URL After The Job Runs
To ping a given URL after the job has executed its tasks, follow the below procedure:
$schedule->command('example')->thenPing($url);
That's all for today. Hope this long examples help you the journey of development.
Read More: What is eloquent serialization in Laravel