【问题标题】:Laravel 8: How to properly return notifications on bladeLaravel 8:如何在刀片上正确返回通知
【发布时间】: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 我真的很感激...
  • 你的意思是实时通知吗?

标签: php laravel laravel-8


【解决方案1】:

基本上,您的错误是由其他通知数据库上没有 'thread' 引起的。 你怎么能防止这种情况发生?只需添加一个 type 来区分通知视图,并且您需要用一个类型告诉所有其他通知可能是其他 hasPostcreateSome 或其他的名称。

public function toDatabase($notifiable)
    {
        return [
            'thread' => $this->thread,
            'user' => $notifiable,
            'type' => 'RepliedThread'
        ];
    }

然后在您的通知刀片中,您只需使用if statement 来检查它是什么类型

@forelse(auth()->user()->unreadNotifications as $notification)
@if($notification->data['type'] == "RepliedThread")
  <a href="">
   {{$notification->data['thread']['title']}}</strong>
</a>
@endif
@empty
no notification
@endforelse

对于实时通知,您需要使用 WebSockets Server,为此有一个第 3 方,它被称为 Pusher 或者如果你想使用自己的 WebSocket 服务器,Laravel 有 Laravel-Websockets。如果你深入挖掘,你会发现Laravel-Echo

【讨论】:

  • 但现在我得到了Undefined index: type
  • 您还需要将类型添加到每个其他通知数据库,以便您的应用知道什么是类型,以及要显示什么。
  • 我不明白你所说的所有其他通知数据库是什么意思,我已经在notifications 表中有type 列,就像图片显示的那样......
  • 前 3 个数据没有导致错误的数据['thread'] 未定义索引错误,因为它们没有线程。
猜你喜欢
  • 2019-09-28
  • 2015-05-06
  • 2023-04-08
  • 2021-03-30
  • 1970-01-01
  • 1970-01-01
  • 2017-01-02
  • 1970-01-01
  • 2017-11-19
相关资源
最近更新 更多