5

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.

1
  • 1
    You shouldn't do that. Commented Oct 18, 2017 at 16:46

2 Answers 2

1

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" }
Sign up to request clarification or add additional context in comments.

Comments

0

You can change the value of type for broadcast and database channels.

For Database channel you must add the method databaseType to your notification. See: https://github.com/laravel/framework/blob/10.x/src/Illuminate/Notifications/Channels/DatabaseChannel.php#L35

For Broadcast channel you must add the method broadcastType to your notification. See: https://github.com/laravel/framework/blob/10.x/src/Illuminate/Notifications/Events/BroadcastNotificationCreated.php#L108

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.