Laravel 通知将一个通知标记为已读
Laravel Notifications Mark One Notification as Read
我是 laravel 通知的新手
我想要什么时候单击通知 link 将我带到发票并且通知应标记为已读
我不知道如何将一个通知标记为已读。
我知道我应该使用通知 ID 将特定通知标记为已读,但我不知道如何在函数中使用它。
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>
控制器:
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'];
}
您只需要将通知的 id 发送到您的控制器并使其成为已读状态,这与您对所有已读状态所做的相同
在 blade
中创建一个 link
<a class="d-flex p-3 border-bottom" href="{{ url('ReadNotification') }}/{{ $notification->data['id'] }}" data-id="{{$notification->id}}" >
为其定义路线
Route::get('ReadNotification/{id}','BlahBlahController@ReadNotification')->name('ReadNotification');
在控制器中
public function ReadNotification($id)
{
$userUnreadNotification = auth()->user()
->unreadNotifications
->where('id', $id)
->first();
if($userUnreadNotification) {
$userUnreadNotification->markAsRead();
}
return back();
}
我是 laravel 通知的新手 我想要什么时候单击通知 link 将我带到发票并且通知应标记为已读 我不知道如何将一个通知标记为已读。 我知道我应该使用通知 ID 将特定通知标记为已读,但我不知道如何在函数中使用它。
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>
控制器:
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'];
}
您只需要将通知的 id 发送到您的控制器并使其成为已读状态,这与您对所有已读状态所做的相同
在 blade
中创建一个 link<a class="d-flex p-3 border-bottom" href="{{ url('ReadNotification') }}/{{ $notification->data['id'] }}" data-id="{{$notification->id}}" >
为其定义路线
Route::get('ReadNotification/{id}','BlahBlahController@ReadNotification')->name('ReadNotification');
在控制器中
public function ReadNotification($id)
{
$userUnreadNotification = auth()->user()
->unreadNotifications
->where('id', $id)
->first();
if($userUnreadNotification) {
$userUnreadNotification->markAsRead();
}
return back();
}