Php Laravel 8:如何在刀片服务器上正确返回通知

Php Laravel 8:如何在刀片服务器上正确返回通知,php,laravel,laravel-8,Php,Laravel,Laravel 8,我正在与Laravel 8合作开发我的项目,这是一个在线论坛,对于这个项目,我想添加一些通知功能,如果有人回答了他在论坛上提出的问题,可以提醒用户 因此,基本上,当有人回答问题时,此方法将运行: public function postAnswer(Question $id) { $validate_data = Validator::make(request()->all(),[ 'answer' => 'required', ])->vali

我正在与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
的受保护变量,并将其分配给
$id
变量,该变量来自:
auth()->user()->notify(new RepliedToThread($id))

在那之后,我试着通过以下方式返回它们:

public function toDatabase($notifiable)
    {
        return [
            'thread' => $this->thread,
            'user' => $notifiable
        ];
    }
最后,我在刀片上添加了以下内容:

<a href="">
   {{$notification->data['thread']['title']}}</strong>
</a>

但现在我得到了这个错误:

ErrorException未定义索引:线程

所以我真的不知道这里出了什么问题!所以,如果你知道如何解决这个问题,请让我知道,我真的很感激你们的任何想法或建议

这也是我的表
通知
,如果您想查看:

这也是blade上的
{{dd($notification)}
的结果:


基本上,您的错误是由其他通知数据库上没有“线程”引起的如何防止这种情况发生?只需添加一个类型来说明不同的通知视图,并且您需要使用其他hasPost、createSome或其他类型的类型名称来说明每个其他通知

public function toDatabase($notifiable)
    {
        return [
            'thread' => $this->thread,
            'user' => $notifiable,
            'type' => 'RepliedThread'
        ];
    }
然后在通知刀片中,您只需使用
if语句
来检查它是什么类型的

@forelse(auth()->user()->unreadNotifications as $notification)
@if($notification->data['type'] == "RepliedThread")
  <a href="">
   {{$notification->data['thread']['title']}}</strong>
</a>
@endif
@empty
no notification
@endforelse
@forelse(auth()->user()->unreadNotifications as$notification)
@如果($notification->data['type']==“RepliedThread”)
@恩迪夫
@空的
不通知
@endforelse

对于实时通知您需要使用WebSocket服务器,有一个第三方,它叫推送器,或者如果您想使用自己的WebSocket服务器,Laravel有
Laravel WebSocket
。如果你深入研究,你会发现
Laravel Echo

那么你想通过实时推送通知吗?@WailanTirajoh是的,但现在我得到了这个错误!好吧,让我在答题帖上回答这个问题,基本上是为了你们应该使用的推送通知event@WailanTirajoh我真的很感激…你是说实时通知吗?但是现在我得到了
未定义的索引:type
!你还需要向其他所有通知数据库添加类型,以便你的应用程序知道类型以及要显示的内容。我不明白你所说的其他所有通知数据库是什么意思,我已经在
notifications
表中设置了
type
列,就像图中显示的那样……前3个数据没有数据['thread']这会导致错误未定义索引错误,因为它们没有线程。OOPS!我知道了,谢谢你,伙计,,,