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.
