laraveleloquentlaravel-bladelaravel-notification

Laravel Notifications Mark One Notification as Read


I'm new to laravel notification I want when to click on the notification the link takes me to the invoice and the notification should be marked as read I don't know how to mark one notification as read. I know that I should take the notification id to mark the specific notification as read but I don't know how to is it in a function.

enter image description here

blade :

   <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>

Controller:

    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'];

        }

Solution

  • Create a link in the blade

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

    Define a route for it

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

    In Controller

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