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

thebigk's avatar
Level 13

Possible to add extra column to database notifications table?

I need to add a few columns to the default database notifications table created by Laravel. I could do that by running the migration. But it doesn't save the data I pass. My best try so far is trying to set $notifiable->column_name inside toArray($notifiable) method.

0 likes
11 replies
rin4ik's avatar

create new migration

php artisan make:migration add_columnName_to_notifications

add column and migrate

thebigk's avatar
Level 13

That was the first step. I've already added the column, but looking for ways to add data to it.

Vilfago's avatar

Retrieve your data, go through each line, and add the needed data on the new column using $model->new_column and save() ?

thebigk's avatar
Level 13

@Vilfago - Are you suggesting that I should create a new DB Notification channel?

dangerwayne's avatar

@thebigk I was wondering if you ever solved this. I'm currently trying to figure this one out.

Calid's avatar

@Neokyuubi Thanks! I got all but I am lost at last part "all you need to do is to set the batch_id on the notification object". Mind sharing how to accomplish that.

UPDATE

class TestNotification extends Notification
{
    use Queueable;

    public function __construct($batch)
    {
        $this->batch = $batch;
        $this->batch_id = $batch->id;
    }
.
.
.
}
1 like
dhavalkakkad's avatar

@calid follow this and you should be good to go.

I read the blog @neokyuubi suggested, but I found a simpler way. Follow this.

The first step is, create a class using

php artisan make:notification customDbNotification

( you can name the class anything you want ). Create a migration and extend the field as you like, in my case it a custom text message named " message_text ".

Now in the new created class, just define a single method send with the following code.

public function send($notifiable, Notification $notification)
  {
    $data = $notification->toDatabase($notifiable);

    // set custom message in another variable and unset it from default array.
    $msg = $data['message_text'];
    unset($data['message_text']);

    // lets create a DB row now with our custom field message text
    
    return $notifiable->routeNotificationFor('database')->create([
        
        'id' => $notification->id,
        'message_text' => $msg, //<-- comes from toDatabase() Method, this is my customised column
        'notifiable_type'=> \Auth::user()->id,
        'type' => get_class($notification),
        'data' => $data,
        'read_at' => null,
    ]);
  }    

Final step. In your current notification class, go to via method and instead of using "database" in array, inject the class we just created. In my case, I am also broadcasting the message, so it ended up looking like this.

public function via($notifiable)
    {
        return [customDbNotification::class,'broadcast']; 
    }

and now in toDatabase method, I returned the custom message, like this.

 return [
            'message_text' => "your custom text message"
        ];

All done. You should have everything in place now.

Let me know if you still need help with this.

Cheers.

3 likes
revilcv's avatar

@dhavalkakkad What is "database" in this line: return $notifiable->routeNotificationFor('database')->create ?? It should be name of my custom database or something or it means create database?

Please or to participate in this conversation.