Php 标记为已读,不按预期更新未读通知计数
我在导航栏中有一个应用内通知图标,显示未读通知的数量 下拉列表中的每个通知都有一个“查看请求”链接。当用户单击此链接时,必须将通知标记为已读,然后根据单击的通知将用户重定向到相关页面。如果用户不想重定向,也可以选择仅将特定通知标记为已读 当用户单击“标记为已读”时,页面将重定向回原处,数字将按预期减少1。但是,当用户单击“查看请求”时,页面将重定向到相关页面,但数量不会减少。我可以在数据库中看到“read_at”值肯定已更新。只有刷新页面后,才会显示数字递减。是否在执行数据库查询之前页面会重定向?我使用单路线封闭来处理这个问题。如果用户单击“标记为已读”,则“leavenr”变量仅作为“null”传递,否则,如果用户单击“查看请求”,则传递实际数字。我的代码如下: web.phpPhp 标记为已读,不按预期更新未读通知计数,php,sql-server,laravel,Php,Sql Server,Laravel,我在导航栏中有一个应用内通知图标,显示未读通知的数量 下拉列表中的每个通知都有一个“查看请求”链接。当用户单击此链接时,必须将通知标记为已读,然后根据单击的通知将用户重定向到相关页面。如果用户不想重定向,也可以选择仅将特定通知标记为已读 当用户单击“标记为已读”时,页面将重定向回原处,数字将按预期减少1。但是,当用户单击“查看请求”时,页面将重定向到相关页面,但数量不会减少。我可以在数据库中看到“read_at”值肯定已更新。只有刷新页面后,才会显示数字递减。是否在执行数据库查询之前页面会重
Route::get('/markasread/{id}/{leavenr}', function($id, $leavenr){
auth()->user()->unreadNotifications->where('id', $id)->first()->markAsRead();
if($leavenr !== "null"){
return view('leave.show', ['leavereqs' => Leave::where('leavenr', $leavenr)->first()]);
} else{
return redirect()->back();
}
})->name('markasread');
app.blade.php
(我省略了大部分冗余代码。下面是与此问题相关的唯一代码)
@如果(auth()->user()->unreadNotifications->count()>0)
{{auth()->user()->unreadNotifications->count()}
@恩迪夫
{{$notification->data['requested_by']}}提交了一个新的{{$notification->data['duration']}日休假请求。
更新:
我注意到,如果我简单地使用returnredirect()->back()
而不是返回视图('leave.show',['leavereqs'=>leave::where('leavenr',$leavenr)->first())代码>它按预期工作。是否有可能Laravel正在缓存app.blade.php文件,而在重定向到其他页面时没有实际刷新它,从而导致计数不更新?我们可以看到->markAsRead()函数吗?@Petay87 markAsRead()方法是Laravel的lightize\Notifications\Notifiable trait使用的默认方法。看到->markAsRead()函数了吗?@Petay87 markAsRead()方法是Laravel的lightize\Notifications\Notifiable trait使用的默认方法。看见
<!-- Show number of unread messages next to notification icon -->
@if(auth()->user()->unreadNotifications->count() > 0)
<span class="badge badge-danger"> {{ auth()->user()->unreadNotifications->count() }} </span>
@endif
<div class="col-sm-10">
<div style="font-size: 90%; padding-bottom:10px; padding-top:10px;">
<b>{{ $notification->data['requested_by'] }}</b> submitted a new <b>{{ $notification->data['duration'] }}</b> day leave request. <a class="notification-link" href="{{ route('markasread', ['id' => $notification->id, 'leavenr' => $notification->data['leavenr']]) }}" style="color:red">View request</a>
</div>
<div style="position:absolute; font-size: 12px;">
<a class="notification-link" href="{{ route('markasread', ['id' => $notification->id, 'leavenr' => "null"]) }}"><i class="fa fa-check"> Mark as read</i></a>
</div>
<div class="text-right" style="font-size: 12px;">
<a class="notification-link" href="#"><i class="fa fa-clock-o"> Created: {{ Carbon\Carbon::parse($notification->created_at)->format('Y/m/d') }}</i></a>
</div>