简体   繁体   English

将单个 Laravel 通知标记为已读

[英]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.数据库中的 ID 设置为char因此是一长串字母和数字。 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.当我在页面上显示通知的 ID 时,它们都返回与数据库中的内容无关的数字,因此当我运行查询以读取通知时,它找不到 ID,因为 ID 不是“ t 匹配。

What is the best method to read a single notification in Laravel?在 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.我正在使用下面的代码进行尝试,但$request->get('id')是不正确的,因为我似乎将 12310 作为 ID 甚至 0 作为 ID。

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

Laravel notifications table's id use CHAR as the default field type. Laravel notifications表的id使用CHAR作为默认字段类型。 So, when you filter for certain id you have to use first() like the following.因此,当您过滤某个 id 时,您必须使用first() ,如下所示。 Since the unreadNotifications is the Illuminate\\Notifications\\DatabaseNotificationCollection由于unreadNotificationsIlluminate\\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 = Auth::user()->unreadNotifications->where('id', $request->get('id'))->first()->get (); $note->markAsRead(); $note->markAsRead();

Laravel notifications table's id uses CHAR as the default field type. Laravel notifications表的 id 使用 CHAR 作为默认字段类型。 So, when you filter for a certain id you have to use first() like the following.因此,当您过滤某个 id 时,您必须使用first() ,如下所示。

My xxx.blade.php is:我的xxx.blade.php是:

 

   <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 :web.php

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

and Finally in my VendorsController@comment_replay function is like below:最后在我的VendorsController@comment_replay函数中,如下所示:


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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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