Here is the SendBirthdayEmailCommand
command created under /App/Commands/
directory
class SendBirthdayEmailCommand extends Command implements ShouldBeQueued {
use InteractsWithQueue, SerializesModels;
public function __construct()
{
}
}
with a handler
class SendBirthdayEmailCommandHandler
in /App/Handlers/
diretory
class SendBirthdayEmailCommandHandler {
public function __construct()
{
//
}
public function handle(SendBirthdayEmailCommand $command)
{
//
$reminders = \App\Reminder::all()->where('reminder_status','=','scheduled')
->where('reminder_set','=','birthday')
->where('reminder_type','=','email')
->where('reminder_time','<=', \Carbon\Carbon::now('Asia/Kolkata'));
foreach ($reminders as $reminder) {
$this->sendEmailNow($reminder);
}
}
public function sendEmailNow($reminder_record)
{
$reminderdata = $reminder_record;
$from = $reminderdata->reminder_from;
$to = $reminderdata->reminder_to;
$msgstring = $reminderdata->reminder_msg;
\Illuminate\Support\Facades\Mail::queueOn(
'birthday_email',
['html' => 'emails.bdaymailhtml'],
$msgstring,
function($message){
$message->from('reminder@example.com', 'Reeminder');
$message->to($to,'')->subject('Happy Birthday to You!');
}
);
}
}
How to Dispatch
this Command
Every 1 Hour from the schedular
Laravel doc only shows example of scheduling console commands, not Commands under App/Commands
diretory
Edit 1
To be more specific, I would like to dispatch the SendBirthdayEmailCommand
from the Schedular
Is that a correct approach? Or do i have to create a console command explicitly and then make a call to my command in App/Commands
P.S. The two command references are confusing. commands
in /App/Console/Commands
are the artisan console commands
then what we call the commands
in /App/Commands
directory with their handler
classes in /App/Handlers
Directory
Edit 2
As suggested by @luceos here https://stackoverflow.com/a/31043897/1679510
I have used closure as below to dispatch the SendBirthdayEmailCommand
from /app/Console/Kernel.php
protected function schedule(Schedule $schedule)
{
$schedule->command('inspire')
->hourly();
$schedule->call(function()
{
$this->dispatch(new App\Commands\SendBirthdayEmailCommand());
})->hourly();
}
And to avail the dispatch()
method in Kernel I have also did following
Availing Command Bus Facade
to Kernel
use Illuminate\Foundation\Bus\DispatchesCommands;
Using the Facade inside Kernel
class
class Kernel extends ConsoleKernel {
use DispatchesCommands;
/* rest of the code */
}
Let's see if that works!! Figuring out how to run cron on xampp to let the schedular run.
Also unsure, where would i get the debug log if there comes any error
Update:
After a bit playing with the Kernel and then running php artisan
Have found that only console commands can be registered in kernel
For Example
class Kernel extends ConsoleKernel {
protected $commands = [
'App\Console\Commands\Inspire',
'\App\Commands\SendBirthdayEmailCommand', /* This will give error */
];
}
The Commands under app/Commands/
directory cannot be registered in kernel, as it only accepts instance of console commands
So that said, If we would like to call app/commands/MyCommand
command in schedule
method with something like $schedule->command('mycmd')
Do we have to create an explicit console command?
You would edit the app/Console/Kernel.php
class and add your command under schedule()
method using:
$schedule->command('birthday-email')->everyHour();
Assuming you named your App\Commands\SendBirthdayEmailCommand
birthday-email
using property $name
.
As you have two classes I think you mixed things up. You would need to move the handle()
method of SendBirthdayEmailCommandHandler
to the SendBirthdayEmailCommand
or call the method from within the command.
If you want to call arbitrary things you can also simply use the call method:
schedule->call(function()
{
// Do some task...
})->hourly();