I suggest to watch
https://laracasts.com/series/whats-new-in-laravel-5-3/episodes/10
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Setup a notification to send an email, save to the database and broadcast.
Sending the email and broadcasting works fine.
Not sure what is wrong. I have other notifications that save to the database, which works fine and the code is exactly the same.
Here is my notification:
<?php
namespace App\Notifications;
use App\Models\Order;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Mail\OrderPlaced as OrderPlacedMail;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Messages\BroadcastMessage;
class OrderPlaced extends Notification implements ShouldQueue
{
use Queueable;
/**
* Information about the order.
*
* @var string
*/
protected $order;
/**
* Information about the subject.
*
* @var string
*/
protected $subject;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct(Order $order, string $subject = 'Order Placed')
{
$this->order = $order->load('user', 'status', 'location', 'order_type', 'shipping_method');
$this->subject = $subject;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable) : array
{
return ['mail', 'database', 'broadcast'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new OrderPlacedMail($this->order))->to($this->order->user->email)->subject($this->subject);
}
/**
* Get the broadcastable representation of the notification.
*
* @param mixed $notifiable
* @return BroadcastMessage
*/
public function toBroadcast($notifiable)
{
$data = [
'order' => $this->order->load('user', 'status', 'location', 'order_type', 'shipping_method'),
];
return (new BroadcastMessage($data))->onQueue('orders.broadcasts');
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toDatabase($notifiable) : array
{
return [
'order' => $this->order->load('user', 'status', 'location', 'order_type', 'shipping_method'),
];
}
}
Any help would be grateful.
Thanks Sean
So I managed to resolve my issue...debugging line by line...
So I was calling my notification like this:
// Sends an order placed notification to the user. Stick the notification in the "orders" queue to run in 5 minutes.
$this->user->notify((new OrderPlaceNotification($event->order))->delay($time)->onQueue('orders.notifications'));
// Sends an order placed notification to the supplier. Sticks the notification in the "orders" queue to run in 5 minutes.
Notification::route('mail', $this->supplierEmail)->notify((new OrderPlaceNotification($event->order))->delay($time)->onQueue('orders.notifications'));
Trying to call Notification::route(...) was my issue.
Notifications were sent by email, saved to the database and broadcasted in the first call. It was my 2nd call that failed all along.
Because I was trying to call the same OrderPlaceNotification in both calls, the 2nd call didn't have a User class/instance associated with it, so saving to the database was always going to fail.
Please or to participate in this conversation.