Laravel comes with a built-in system for notifications. There are various channels for sending these notifications, such as - email, SMS & more. One of them is via the database wherein the notifications are stored and can be read as per your need.
Disclaimer - I'm assuming you are somewhat familiar with Laravel.
As per Laravel 8's official docs on Notifications, you can add 'data' to your notification by utilizing toDatabase()
or toArray()
method.
public function toDatabase($notifiable)
{
return [
'product_name' => $this->product->name,
'user_name' => $this->user->name,
];
}
& you can then display the notification in your views (via Controller).
<div>
{{ $notification->data['user_name'] }} ordered {{ $notification->data['product_name'] }}
</div>
But what if the product's name or user's name was updated? This is why you might want to store the IDs instead.
public function toDatabase($notifiable)
{
return [
'product_id' => $this->product->id,
'user_id' => $this->user->id,
];
}
Now, how about displaying the data in this approach? Well, you can fetch the respective product and user models in a controller like...
public function index() {
$notifications = $user->notifications;
foreach($notifications as $notification) {
// populate data from models
...
}
return view('notifications', compact('notifications'));
}
...wherein you can read the notification type and load respective models.
But should this be a concern of a controller? One of the simpler ways you can handle this is macroing on the DatabaseNotificationCollection
inside of, perhaps, your AppServiceProvider
like...
public function boot()
{
//
DatabaseNotificationCollection::macro('addModels', function () {
return $this->each(function ($notification) {
// populate data from models
...
});
});
}
which then allows you to optionally load models for the notification collection via a controller like...
public function index() {
$notifications = $user->notifications->addModels();
return view('notifications', compact('notifications'));
}
I've implemented a similar approach to Notifications for the purpose of demonstration on my GitHub repo. I have a custom convention in place. Feel free to have a look and add to.
Liked what you read? Support me here: https://www.buymeacoffee.com/zaxwebs
Top comments (2)
That's just fantastic
Thank you very much !!!