【发布时间】:2021-04-24 10:25:05
【问题描述】:
我正在使用 Laravel 8 开发我的在线论坛项目,我想为这个项目添加一些通知功能,如果有人回答了他在论坛上提出的问题,可以提醒用户。
所以基本上,当有人回答问题时,这个方法就会运行:
public function postAnswer(Question $id)
{
$validate_data = Validator::make(request()->all(),[
'answer' => 'required',
])->validated();
$answer = Answer::create([
'answer' => $validate_data['answer'],
'user_id' => auth()->user()->id,
'question_id' => $id,
]);
auth()->user()->notify(new RepliedToThread($id)); // making new notification
return back();
}
然后,我创建了这个名为 RepliedToThread.php 的通知:
class RepliedToThread extends Notification
{
use Queueable;
protected $thread;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct($id)
{
$this->thread = $id;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['database'];
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toDatabase($notifiable)
{
return [
'thread' => $this->thread,
'user' => $notifiable
];
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}
如您所见,我定义了一个名为 $thread 的受保护变量,并将其分配给来自 auth()->user()->notify(new RepliedToThread($id)); 的 $id 变量
之后,我尝试通过以下方式退货:
public function toDatabase($notifiable)
{
return [
'thread' => $this->thread,
'user' => $notifiable
];
}
最后,我将这个添加到刀片中:
<a href="">
{{$notification->data['thread']['title']}}</strong>
</a>
但现在我得到了这个错误:
ErrorException 未定义索引:线程
所以我真的不知道这里出了什么问题!所以如果你知道如何解决这个问题,请告诉我,我非常感谢你们的任何想法或建议......
这也是我的桌子notifications,如果你想看:
这也是blade上{{ dd($notification) }}的结果:
【问题讨论】:
-
所以你想通过实时推送通知?
-
@WailanTirajoh 是的,但现在我得到了这个错误!
-
好吧,让我在回答帖子上回答这个问题,基本上对于推送通知,您应该使用事件
-
@WailanTirajoh 我真的很感激...
-
你的意思是实时通知吗?