Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add custom value for "type" column in laravel default notification table

I am using laravel's default notification system. The type column of the table gets populated by full class path like "App\Notifications\Upvoted" . I am only filling the data column by myself like this:

public function toDatabase($notifiable)
 {
     return [
         "post" => $this->post,
         "user" => Auth::user()
     ];
 }

How can I add custom value for "type" column too.

As I am new to Laravel your help will be greatly appreciated.

like image 662
Romy Gomes Avatar asked Nov 07 '22 15:11

Romy Gomes


1 Answers

You can not do this because the type Field is following morph rules in the Laravel framework.

If you need to save extra data in Notification Table you can pass in an array and then add as a JSON field in data field.

For instance you return:

public function toArray($notifiable)
{
    return [
        'post_id' => $this->post_id,
        'user_id' => Auth::user()->id,
    ];
}

And the result in notifacation data field will be:

{ "post_id": "2", "user_id": "1" }
like image 79
Hamed Yarandi Avatar answered Nov 14 '22 21:11

Hamed Yarandi