I created a laravel notification class with
php artisan make:notification NewUserNotification
JavaScript
x
class NewUserNotification extends Notification
{
use Queueable;
protected $cost;
private $order_id;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct($cost, $order_id)
{
$this->cost = $cost;
$this->order_id = $order_id;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return [CustomDbChannel::class];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return IlluminateNotificationsMessagesMailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->line('The introduction to the notification.')
->action('Notification Action', url('/'))
->line('Thank you for using our application!');
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toDatabase($notifiable)
{
$a = $this->cost;
return [
'cost' => $a,
'order_id' => $this->order_id,
];
}
public function toArray($notifiable)
{
return [
'cost' => $this->cost,
];
}
}
then made a custom DbChannel to add order id
JavaScript
class CustomDbChannel
{
public function send($notifiable, Notification $notification)
{
$data = $notification->toDatabase($notifiable);
return $notifiable->routeNotificationFor('database')->create([
'id' => $notification->id,
//customize here
'order_id' => $data['order_id'], //<-- comes from toDatabase() Method below
'type' => get_class($notification),
'data' => $data,
'read_at' => null,
]);
}
}
Everything is working perfectly, I am able to access Auth::user->notifications but not the order, I have the order_id as a foreign key and able to access the order in phpMyAdmin by clicking on it, I’ve tried this but its giving me null
$notifications->order
Advertisement
Answer
You could use $order = Order::find($data['order_id'])
presuming you have the Order
class imported into your namespace. I think that’s probably the simplest way.