Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

hjortur17's avatar

Not sending out Notification through a job

Hi, I'm trying to send an email 5 days prior a customer comes. But I don't receiving the Notification when I try to send it out using a job which runs every day. Any reason why? (Locally I run the job every minute).

It works to send out this Notification manually, basically created a test route which does the exact same thing and the DispatchUsefulInfoEmail and it worked. I have php artisan queue:work running in the terminal but it seems as it's not receiving anything. Always the same message: INFO Processing jobs from the [default] queue.

Here is Kernel:

$schedule->job(new DispatchUsefulInfoEmail)->everyMinute();

Here is DispatchUsefulInfoEmail :

public function handle()
    {
        // Retrieve rentals that start within the next 5 days
        $rentals = Rental::where('pick_up_date', '<=', now()->addDays(5)->format('d/m/Y'))
            ->where('status', BookingStatusEnum::CREATED)
            ->get();

        $rentals->each(function ($rental) {
            // Send notification
            Notification::route('mail', $rental->customer->email_address)->notify(new UsefulInfoNotification($rental));
        });
    }

Here is DispatchUsefulInfoEmail :

public function toMail($notifiable)
    {
        $rental = $this->rental;

        return (new MailMessage)
            ->subject('Useful Information')
            ->greeting('Hello ' . $rental->customer->name . '!')
            ->line('Here is some useful information about your rental:')
            ->line('Rental ID: ' . $this->rental->id)
            ->line('Pick-up Date: ' . $this->rental->pick_up_date)
            ->line('Return Date: ' . $this->rental->drop_off_date)
            ->line('Thank you for using our services!');
    }
0 likes
7 replies
LaryAI's avatar
Level 58

It seems like the job is not being dispatched properly. One possible solution is to manually dispatch the job in the test route and see if it works. If it does, then the issue might be with the scheduling.

Another possible solution is to check if the queue driver is set to "database" in the .env file. If it is not, then change it to "database" and run the migration command to create the necessary tables for the queue.

Also, make sure that the job is properly serialized and unserialized. One way to do this is to implement the ShouldQueue interface in the job class.

Here's an updated version of the DispatchUsefulInfoEmail job class that implements the ShouldQueue interface:

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class DispatchUsefulInfoEmail implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public function handle()
    {
        // Retrieve rentals that start within the next 5 days
        $rentals = Rental::where('pick_up_date', '<=', now()->addDays(5)->format('d/m/Y'))
            ->where('status', BookingStatusEnum::CREATED)
            ->get();

        $rentals->each(function ($rental) {
            // Send notification
            Notification::route('mail', $rental->customer->email_address)->notify(new UsefulInfoNotification($rental));
        });
    }
}

Note that the ShouldQueue interface is added and the job class now uses the Queueable, InteractsWithQueue, and SerializesModels traits.

hjortur17's avatar

@LaryAI - It worked to Dispatch it manually. But still not working through the job

Shafqat_ali's avatar

Did you setup supervisor on server to keep running queue worker?

mvd's avatar

@hjortur17 so the DispatchUsefulInfoEmail does work with a test route.

Did you also set the crontab on your server or manually run php artisan schedule:run?

1 like
hjortur17's avatar

@mvd - Sorry for the late response, by running the php artisan schedule:run I was able to send the email through the job

1 like
Snapey's avatar

whats with the Notification::route. ?

why not just dispatch the job

btw your logic seems fundamentally flawed. If I book a place for two weeks time, i'm going to be notified every single day until 5 days before?

You should also eager load customer to avoid n+1 issues

hjortur17's avatar

@Snapey - The Notification::route was the only think I was able to get working.

I agree, I still need to figure out a better solution to solve the issue so a customer only receives the email once. I will most likely place it into a table but if you have a better solution it would be much appreciated :)

How would I go about it to eager load the customer?

Please or to participate in this conversation.