【问题标题】:Laravel Notifications Mark One Notification as ReadLaravel 通知将一个通知标记为已读
【发布时间】:2021-07-19 08:18:37
【问题描述】:

我是 laravel 通知的新手 我想何时点击通知链接将我带到发票,通知应标记为已读 我不知道如何将一个通知标记为已读。 我知道我应该使用通知 id 将特定通知标记为已读,但我不知道如何在函数中使用它。

刀片:

   <div id="unreadNotifications">
                            @foreach (auth()->user()->unreadNotifications as $notification)
                            
                                <div class="main-notification-list Notification-scroll mark-as-read"  >
                                    <a class="d-flex p-3 border-bottom"
                                        href="{{ url('InvoicesDetails') }}/{{ $notification->data['id'] }}"  data-id="{{$notification->id}}" >
                                    
                                            <div class="notifyimg ">
                                                <i class="la la-file-alt text-pink text-center"></i>
                                            </div>
                                            <div class="ml-3">
                                                <h5 class="notification-label mb-1">
                                                    {{ $notification->data['title'] }}
                                                    {{ $notification->data['user'] }}
                                                </h5>
                                                <div class="notification-subtext">{{ $notification->created_at }}
                                                </div>
                                            </div>
                                      
                                    </a>
                                </div>
                            @endforeach

                        </div>

控制器:

    public function MarkAsRead_all (Request $request)
    {

        $userUnreadNotification= auth()->user()->unreadNotifications;

        if($userUnreadNotification) {
            $userUnreadNotification->markAsRead();
            return back();
            
        }


    }
    

    

    public function unreadNotifications_count()

    {
        return auth()->user()->unreadNotifications->count();
    }

    public function unreadNotifications()

    {
        foreach (auth()->user()->unreadNotifications as $notification){

return $notification->data['title'];

        }

【问题讨论】:

    标签: laravel eloquent laravel-blade laravel-notification


    【解决方案1】:

    在刀片中创建一个链接

    <a class="d-flex p-3 border-bottom" href="{{ url('ReadNotification') }}/{{ $notification->data['id'] }}"  data-id="{{$notification->id}}" >
    

    为它定义一条路线

    Route::get('ReadNotification/{id}','BlahBlahController@ReadNotification')->name('ReadNotification');
    

    在控制器中

    public function ReadNotification($id)
    {
      $userUnreadNotification = auth()->user()
                                      ->unreadNotifications
                                      ->where('id', $id)
                                      ->first();
        
      if($userUnreadNotification) {
        $userUnreadNotification->markAsRead();
      }
      return back();
    }
    

    【讨论】:

      【解决方案2】:

      您只需将通知的 id 发送到您的控制器并使其为已读,它与您为所有已读所做的相同

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-05
        • 2017-10-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-03
        相关资源
        最近更新 更多