Artisan commands can be used in many things, here are few examples: –
1. Managing database migrations and seeding
2. Publishing package asset.
3. Generate boilerplate code for new controllers and migrations
4. Create proper code skeletons
5. Insert user details in database.
6. Send feedback form to user after couple of months
7. Send a reminder to the user for tasks etc.
8. Send email and SMS notifications to the user.
Command ‘php artisan list’ – When you fire this command in your terminal, you will get the list of commands which can be used in your Laravel project. In terminal, you will get Command with its description, with the help of description anyone can easily understand the use of command and its purpose. You will get, inbuilt and custom, both the commands in your terminal.
In addition to the commands provided with Artisan, you may also create your own custom commands. Commands are stored in the app/Console/Commands folder.
Following command can be used to create custom commands.
php artisan make:command command name
Custom commands are used when we need to check anything or track the details or insert batch of data, send notification and feedback form to a user after couple of months.
I. After generating the command, you need to fill out the signature & description of the class. Later, it will be used to display the command in terminal list.
II. The handle method will be called when your command is executed.
III. You may place your command logic in handle() method.
These three points explain the structure of commands, how you can write the commands and the fundamental structure of the commands. All the logical part of command available in handle method (method.handle), which is called when the command gets executed in the terminal. Basically you need to take care of handle() method, signature and description part. Once you have done with these three points, you need to register your command and it’s ready to use.
1. Make sure Laravel is installed in your system.
2. Open up console and run the following command – php artisan make:command AppointmentNotificationOneDay
This command will generate file with the name of AppointmentNotificationOneDay.php file
use Illuminate\Support\Facades\Mail;
use App\Mail\SendAppointmentMail;
use Carbon\Carbon;
class AppointmentNotificationOneDay extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = ‘appointment:notificationoneday’;
/**
* The console command description.
*
* @var string
*/
protected $description = ‘Send mail notification to user before 24hrs of appointment schedule.’;
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
// Logic will be written here
}
}
This file is generated in app/Console/Commands directory with the name of command, this class is always extending with Command class. Using this you can easily create as many files as you want.
3. Now its time to register our command, because it’s not registered yet. To register your command, navigate to app/console and open kernel.php file and add the below mention code in $commands array.App\Console\Commands\AppointmentNotificationOneDay
So kernel.php looks like:-
namespace App\Console; use Illuminate\Console\Scheduling\Schedule; use Illuminate\Foundation\Console\Kernel as ConsoleKernel; class Kernel extends ConsoleKernel { /** * The Artisan commands provided by your application. * * @var array */ protected $commands = [ 'App\Console\Commands\AppointmentNotificationOneDay' ]; /** * Define the application's command schedule. * * @param \Illuminate\Console\Scheduling\Schedule $schedule * @return void */ protected function schedule(Schedule $schedule) { $schedule->command('appointment:notificationoneday')->hourly();
}
/**
* Register the Closure based commands for the application.
*
* @return void
*/
protected function commands()
{
require base_path(‘routes/console.php’);
}
}
Congrats, now our command is registered on a console and we are ready to use this command in our application and website.
4. Run php artisan list command in your terminal, it will show the command name with the signature & a description which was previously defined in app/Console/Commands.
