[英]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.