D
D
Dimon3x2019-01-23 20:52:11
Laravel
Dimon3x, 2019-01-23 20:52:11

Why does Call to undefined method routeNotificationFor() throw an error?

I make notifications.
So it's all right

public function sendNotification()
    {
         $user = User::find(1);
        
        
        $details = [
            'user_id' => Auth::id(),
            'comment_id' => 4444,
        ];
  
        Notification::send($user, new MyFirstNotification($details));
   
        dd('done');
    }

but if you replace it with something it gives an error
$user = User::find(1);
$user = Comment::find(1);
Call to undefined method App\Comment::routeNotificationFor()

I understand that the first argument for Notification::send is responsible for the notifiable_type ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex Wells, 2019-01-23
@Alex_Wells

See Alexey Sklyarov 's answer . Notifications in Laravel are designed to be sent to users, you cannot send them "globally" only if you literally send a separate notification to each user. In view of this, the User model by default uses the Notifiable trait, which inside contains the routeNotificationFor method, which is responsible for linking user data (phone number, email, database relays) and the notifications themselves.
Yes, the first argument is notifiable_id & notifiable_type. Actually, by default, this is always the user ID and the value from the morph map for the user model, or the namespace + user class name.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question