[英]Mark a single Laravel notification as read
我的应用程序中有一个页面,该页面显示用户当前拥有的所有未读通知的列表,并且每个通知都有一个已读图标。
数据库中的 ID 设置为char
因此是一长串字母和数字。 当我在页面上显示通知的 ID 时,它们都返回与数据库中的内容无关的数字,因此当我运行查询以读取通知时,它找不到 ID,因为 ID 不是“ t 匹配。
在 Laravel 中读取单个通知的最佳方法是什么? 我正在使用下面的代码进行尝试,但$request->get('id')
是不正确的,因为我似乎将 12310 作为 ID 甚至 0 作为 ID。
Auth::user()->unreadNotifications->where('id', $request->get('id'))->markAsRead()
Laravel notifications
表的id
使用CHAR
作为默认字段类型。 因此,当您过滤某个 id 时,您必须使用first()
,如下所示。 由于unreadNotifications
是Illuminate\\Notifications\\DatabaseNotificationCollection
$notificationId = request('notification_id');
$userUnreadNotification = auth()->user()
->unreadNotifications
->where('id', $notificationId)
->first();
if($userUnreadNotification) {
$userUnreadNotification->markAsRead();
}
上面的函数将使用第一种方法返回一个数组:$note = Auth::user()->unreadNotifications->where('id', $request->get('id'))->first()->get (); $note->markAsRead();
Laravel notifications
表的 id 使用 CHAR 作为默认字段类型。 因此,当您过滤某个 id 时,您必须使用first()
,如下所示。
我的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>
在web.php
:
Route::get('comment-replay/{id}','VendorsController@comment_replay')->name('commentReplay');
最后在我的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.