What have you tried so far?
How use Custom Database Notification Model
Hi, I need to change my Database Notification model, so I'm going to do it. I want to use my custom model. My goal is to add new methods and, for example, change the format of the date as follows: protected $dateFormat = 'Y-m-d H:i:s.u';
I searched so much and did not try anything yet. I'm just looking to be able to use my personalized model for Notification.
Did you read the docs?
Documentation: https://laravel.com/docs/5.6/notifications#sending-notifications
Yes . My problem is that I want to add Milliseconds to Timestamp and this is necessary to code protected $dateFormat = 'Y-m-d H:i:s.u'; in the model
DatabaseNotification.php I add, but this is not the right solution. It should be rewritten model.
Aah oke, well the DatabaseNotification is pretty tied to the framework, so not sure if you can extend it!
Note that you're not the only one with this question: https://github.com/laravel/ideas/issues/191
@bobbybouwmann tanks . I used this method :
class NewNotifications extends DatabaseNotification {
// override
protected $dateFormat = 'Y-m-d H:i:s.u';
}
use Notifiable Trait on User model :
/**
* Get the entity's notifications.
*/
public function notifications()
{
return $this->morphMany(NewNotifications::class, 'notifiable')
->orderBy('created_at', 'desc');
}
@Bvk Yes indeed, but this is the notification class itself, it's not the DatabaseNotification class. You can now add milliseconds, but that will never be saved in the database afaik
Create and use your own Notification model and Notifiable trait and then use your own Notifiable trait in your (User) models.
App\Notifiable.php:
namespace App;
use Illuminate\Notifications\Notifiable as BaseNotifiable;
trait Notifiable
{
use BaseNotifiable;
/**
* Get the entity's notifications.
*/
public function notifications()
{
return $this->morphMany(Notification::class, 'notifiable')
->orderBy('created_at', 'desc');
}
}
App\Notification.php:
namespace App;
use Illuminate\Notifications\DatabaseNotification;
class Notification extends DatabaseNotification
{
// ...
}
App\User.php:
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
// ...
}
@KAMALKHAN - Hi sir, I have the same problem.... here is the full details.. https://laracasts.com/discuss/channels/laravel/real-time-notification-what-is-the-proper-implementation
I hope you idea for this.. Thanks in advance.
@BOBBYBOUWMANN - Hi sir it's me again... I have the same problem in Laravel Notification but in my case is where to put the webhook URL is it in route or in notification.php?
here is the full issue.
This is an old post, but for posterity I'll share my solution.
The problem with adding customizing columns in the DatabaseNotification model is that Laravel handles creating these model instances automatically when running the notification. This logic is defined in \Illuminate\Notifications\Channels\DatabaseChannel::buildPayload.
So to get this working, we need to customize the DatabaseChannel class and override the instance in the service container.
\1. Create a new DatabaseChannel class and override the buildPayload method. I placed it in \App\Broadcasting but you can put it wherever you like:
<?php
namespace App\Broadcasting;
use Illuminate\Notifications\Channels\DatabaseChannel as IlluminateDatabaseChannel;
use Illuminate\Notifications\Notification;
class DatabaseChannel extends IlluminateDatabaseChannel
{
protected function buildPayload($notifiable, Notification $notification): array
{
$data = $this->getData($notifiable, $notification);
return [
'id' => $notification->id,
'type' => $notification::class,
'data' => $data,
'read_at' => null,
// Customize any columns
];
}
}
\2. Override the DatabaseChannel instance used by the service container in your AppServiceProvider::boot method
<?php
namespace App\Providers;
use App\Broadcasting\DatabaseChannel;
use Illuminate\Notifications\Channels\DatabaseChannel as IlluminateDatabaseChannel;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
...
public function boot(): void
{
$this->app->instance(IlluminateDatabaseChannel::class, new DatabaseChannel());
}
...
}
Please or to participate in this conversation.