I have this problem with Laravel 11 that I can't solve:
I create a command to retrieve data to send by email and this work correctly:
class SendEvent5Reminder extends Command {
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'send:send-event5-reminder';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Execute the console command.
*/
public function handle() {
// Get today's date
$today = Carbon::today();
// Calculate the date 5 days from today
$backDate = $today->copy()->subDays(5);
// Find all events expiring within 5 days
$events = Event::whereBetween('start', [$backDate->format('Y-m-d'), $today->format('Y-m-d')])->get();
// Check if there are any events found
if ($events->isEmpty()) {
Log::info('No events expiring within 5 days.');
return 0;
}
// Get the administrator user
$adminUser = User::get();
if (!$adminUser) {
Log::info('No administrator user found.');
return 1;
}
// Dispatch the job to send reminder emails
foreach ($events as $sendEvent) {
RemindEmail5Job::dispatch($adminUser, $sendEvent);
}
Log::info('Reminder emails sent successfully.');
return 0;
}
}
Then I create the Job:
class RemindEmail5Job implements ShouldQueue {
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $adminUser;
protected $sendEvent;
/**
* Create a new job instance.
*/
public function __construct($adminUser, Event $sendEvent) {
$this->adminUser = $adminUser;
$this->sendEvent = $sendEvent;
}
/**
* Execute the job.
*/
public function handle(): void {
Log::info('TEST: Start Job email!');
$email = new Remind5Event($this->sendEvent);
Mail::to($this->adminUser->first()->email)->send($email);
>event));
}
}
But the handle() method don't work and it seems that this method is not even taken into consideration, in fact the log put there for testing is not written.
I just can't figure out where I'm going wrong.
Sending test emails via tinker works correctly, but after many tests the job doesn't work. Why? This all seems correct, but it doesn't go through the handle() method, I just don't understand.
After banging my head against the wall and trying a lot of things for days, suddenly the solution appeared and it is simple: do not use job because the handle() method in the job (used in this way) does not work (it's a bug?... I don't know).
Instead call the email class, for sending email, directly from the handle() method of the artisan command, like this (inside the foreach in this case)
namespace App\Console\Commands;
use App\Models\Event;
use App\Models\User;
use App\Mail\RemindEvent;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
class SendEvent5Reminder extends Command {
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'send:send-event5-reminder';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Execute the console command.
*/
public function handle() {
// Get today's date
$today = Carbon::today();
// Calculate the date 5 days from today
$backDate = $today->copy()->subDays(5);
// Find all events expiring within 5 days
$events = Event::whereBetween('start', [$backDate->format('Y-m-d'), $today->format('Y-m-d')])->get();
// Check if there are any events found
if ($events->isEmpty()) {
Log::info('No events expiring within 5 days.');
return 0;
}
// Get the administrator user
$adminUser = User::get();
if (!$adminUser) {
Log::info('No administrator user found.');
return 1;
}
// Dispatch the job to send reminder emails
foreach ($events as $sendEvent) {
$email = new RemindEvent($sendEvent);
Mail::to($adminUser->first()->email)->send($email);
}
Log::info('Reminder emails sent successfully.');
return 0;
}
}