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

Bvk's avatar
Level 2

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';

0 likes
11 replies
Bvk's avatar
Level 2

I searched so much and did not try anything yet. I'm just looking to be able to use my personalized model for Notification.

Bvk's avatar
Level 2

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.

Bvk's avatar
Level 2

@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');
    }
bobbybouwmann's avatar

@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

KamalKhan's avatar

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;

    // ...
}
1 like
SleeplessDev's avatar

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());
    }
   
    ...
}
4 likes

Please or to participate in this conversation.