Laravel 调用undefined方法Illumb\Database\Eloquent\Relations\BelongToMany::routeNotificationFor()
我正在构建一个消息传递系统,在设置回复时通知会话中的每个用户 MessageNotification.phpLaravel 调用undefined方法Illumb\Database\Eloquent\Relations\BelongToMany::routeNotificationFor(),laravel,notifications,laravel-6,Laravel,Notifications,Laravel 6,我正在构建一个消息传递系统,在设置回复时通知会话中的每个用户 MessageNotification.php class MessageNotification extends Notification { use Queueable; /** * Get the notification's delivery channels. * * @param mixed $notifiable * @return array */
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);
}
错误
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);
}
调用undefined方法Illumb\Database\Eloquent\Relations\BelongToMany::routeNotificationFor()
额外信息
由于需要同时使用LaravelSparks通知系统和Laravels库存通知系统,我不得不建立一个定制的应报告特征
自定义通知特征
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');
}
}
还要注意,$receiver->notify(newmessagenotification())代码>在向一个用户发送通知时工作正常。我在这方面看到的唯一其他解决方案是:
我试图实现它,但我使用的是一个数据库通道,所以它不会有什么不同 这里的这一行:
$users=$conversation->participants()代码>
将$users
变量设置为QueryBuilder实例(假设您使用的是传统的Laravel关系),而不是用户集合。这是因为关系末尾的()
构建了查询,但尚未运行它。因此,当您调用Notification::send($users,etc…)
时,您没有传递用户集合;您正在传入QueryBuilder对象
请尝试以下方法:
$users=$conversation->参与者代码>
同样,这是假设会话模型上的参与者方法是标准的laravel关系 这里的这一行:
$users=$conversation->participants()代码>
将$users
变量设置为QueryBuilder实例(假设您使用的是传统的Laravel关系),而不是用户集合。这是因为关系末尾的()
构建了查询,但尚未运行它。因此,当您调用Notification::send($users,etc…)
时,您没有传递用户集合;您正在传入QueryBuilder对象
请尝试以下方法:
$users=$conversation->参与者代码>
同样,这是假设会话模型上的参与者方法是标准的laravel关系