调用未定义的方法 Illuminate\Database\Eloquent\Relations\BelongsToMany::routeNotificationFor()
Call to undefined method Illuminate\Database\Eloquent\Relations\BelongsToMany::routeNotificationFor()
我正在构建一个消息系统,在设置回复时通知对话中的每个用户。
MessageNotification.php
class MessageNotification extends Notification
{
use Queueable;
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['database'];
}
public function toArray($notifiable)
{
return [
'data' => 'Messenger notification'
];
}
}
InboxController
public function reply($hashedId, Request $request)
{
$this->validate($request, [
'body' => 'required',
]);
$conversation = Conversation::where('hashed_id', $hashedId)->first();
$users = $conversation->participants();
//dd($conversationUserIds);
$notifications = Notification::send($users, new MessageNotification());
$message = $conversation->messages()->create([
'sender_id' => auth()->user()->id,
'body' => $request->body,
]);
return new MessageResource($message);
}
错误
Call to undefined method Illuminate\Database\Eloquent\Relations\BelongsToMany::routeNotificationFor()
额外信息
由于需要同时使用 Laravel Sparks 通知系统和 Laravels 股票通知系统,我不得不构建一个自定义的 Notifiable 特征。 Tutorial I got code from.
自定义通知特征
namespace App\Traits;
use Illuminate\Notifications\Notifiable as BaseNotifiable;
use App\Notifications\DatabaseNotification;
trait Notifiable {
use BaseNotifiable;
public function notifications() {
return $this->morphMany(DatabaseNotification::class, 'notifiable')->orderBy('created_at', 'desc');
}
}
另请注意,$reciever->notify(new MessageNotification());
在向一个用户发送通知时工作正常。我看到的唯一其他解决方案是:https://laracasts.com/discuss/channels/code-review/call-to-undefined-method-routenotificationfor-while-sending-email-to-multiple-users
我试图实现它,但我使用的是数据库通道,所以应该不会有什么不同。
这里是这一行:
$users = $conversation->participants();
会将 $users
变量设置为 QueryBuilder 实例(假设您使用的是常规 Laravel 关系),而不是用户集合。这是因为关系末尾的 ()
构建了查询,但还没有 运行。因此,当您调用 Notification::send($users, etc...)
时,您并没有传递用户集合;您正在传递一个 QueryBuilder 对象。
试试这个:
$users = $conversation->participants;
再次 - 这是假设对话模型上的参与者方法是标准 laravel 关系。
我正在构建一个消息系统,在设置回复时通知对话中的每个用户。
MessageNotification.php
class MessageNotification extends Notification
{
use Queueable;
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['database'];
}
public function toArray($notifiable)
{
return [
'data' => 'Messenger notification'
];
}
}
InboxController
public function reply($hashedId, Request $request)
{
$this->validate($request, [
'body' => 'required',
]);
$conversation = Conversation::where('hashed_id', $hashedId)->first();
$users = $conversation->participants();
//dd($conversationUserIds);
$notifications = Notification::send($users, new MessageNotification());
$message = $conversation->messages()->create([
'sender_id' => auth()->user()->id,
'body' => $request->body,
]);
return new MessageResource($message);
}
错误
Call to undefined method Illuminate\Database\Eloquent\Relations\BelongsToMany::routeNotificationFor()
额外信息
由于需要同时使用 Laravel Sparks 通知系统和 Laravels 股票通知系统,我不得不构建一个自定义的 Notifiable 特征。 Tutorial I got code from.
自定义通知特征
namespace App\Traits;
use Illuminate\Notifications\Notifiable as BaseNotifiable;
use App\Notifications\DatabaseNotification;
trait Notifiable {
use BaseNotifiable;
public function notifications() {
return $this->morphMany(DatabaseNotification::class, 'notifiable')->orderBy('created_at', 'desc');
}
}
另请注意,$reciever->notify(new MessageNotification());
在向一个用户发送通知时工作正常。我看到的唯一其他解决方案是:https://laracasts.com/discuss/channels/code-review/call-to-undefined-method-routenotificationfor-while-sending-email-to-multiple-users
我试图实现它,但我使用的是数据库通道,所以应该不会有什么不同。
这里是这一行:
$users = $conversation->participants();
会将 $users
变量设置为 QueryBuilder 实例(假设您使用的是常规 Laravel 关系),而不是用户集合。这是因为关系末尾的 ()
构建了查询,但还没有 运行。因此,当您调用 Notification::send($users, etc...)
时,您并没有传递用户集合;您正在传递一个 QueryBuilder 对象。
试试这个:
$users = $conversation->participants;
再次 - 这是假设对话模型上的参与者方法是标准 laravel 关系。