【发布时间】:2020-02-27 00:52:56
【问题描述】:
我正在构建一个消息传递系统,该系统会在设置回复时通知对话中的每个用户。
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'
];
}
}
收件箱控制器
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);
}
错误
调用未定义的方法 Illuminate\Database\Eloquent\Relations\BelongsToMany::routeNotificationFor()
额外信息
由于需要同时使用 Laravel Sparks 通知系统和 Laravel 库存通知系统,我不得不构建一个自定义的 Notifiable trait。 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
我试图实现这一点,但我使用的是数据库通道,所以它不应该有所作为。
【问题讨论】:
标签: laravel notifications laravel-6