简体   繁体   中英

Mark a single Laravel notification as read

I have a page in my application that displays a list of all of the notifications a user currently has that are unread and each notification will have a read icon.

The ID in the database is set to char so is a long string of letters and numbers. When I display the id of the notifications on the page, they all return numbers that in no way relate to what is in the database so when I run a query to read a notification, it can't find the ID because the ID isn't matching.

What is the best method to read a single notification in Laravel? I'm using the code below to try, but it's the $request->get('id') that's incorrect as I seem to get 12310 as an ID and even 0 for some of them.

Auth::user()->unreadNotifications->where('id', $request->get('id'))->markAsRead()

Laravel notifications table's id use CHAR as the default field type. So, when you filter for certain id you have to use first() like the following. Since the unreadNotifications is the Illuminate\\Notifications\\DatabaseNotificationCollection

$notificationId = request('notification_id');

$userUnreadNotification = auth()->user()
                            ->unreadNotifications
                            ->where('id', $notificationId)
                            ->first();

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

the function above will return an array use first method : $note = Auth::user()->unreadNotifications->where('id', $request->get('id'))->first()->get(); $note->markAsRead();

Laravel notifications table's id uses CHAR as the default field type. So, when you filter for a certain id you have to use first() like the following.

My xxx.blade.php is:

 

   <li class="nav-item dropdown">
        <a id="navbarDropdown" class="nav-link dropdown-toggle" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" v-pre>
            <span class="glyphicon glyphicon-globe"></span>Notifications<span class="badge">{{ count(Auth::user()->unreadNotifications) }}</span>

        </a>

        <div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdown">
            @foreach(Auth::user()->unreadNotifications as $notification)
            <a href="{{route('vendor.commentReplay',[ 'id' => $notification->id])}}" class="dropdown-item" >
                {{$notification->data['user']['email']}} commented on <strong> {{$notification->data['CommentDetails']['comment']}}</strong>
            </a>
            @endforeach
        </div>
    </li>

In web.php :

    Route::get('comment-replay/{id}','VendorsController@comment_replay')->name('commentReplay');

and Finally in my VendorsController@comment_replay function is like below:


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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM