标记为已读未按预期更新未读通知计数

时间:2019-07-17 14:07:42

标签: php sql-server laravel

我在导航栏中有一个应用内通知图标,该图标显示未读通知的数量。

enter image description here

下拉菜单中的每个通知都有一个“查看请求”链接。用户单击此链接时,必须将通知标记为已读,然后根据单击的通知将用户重定向到相关页面。用户也可以选择仅将特定的通知标记为已读,如果他们不想被重定向。

enter image description here

当用户单击“标记为已读”时,页面将重定向回去,并且数字按预期减少1。但是,当用户单击“查看请求”时,页面将重定向到相关页面,但数量不会减少。我可以在数据库中看到“ read_at”值确实已更新。仅刷新页面后,才会显示数字减量。可以在执行数据库查询之前页面重定向吗?我使用单个路由关闭来处理此问题。如果用户单击“标记为已读”,则将“清除者”变量简单地传递为“空”,否则,如果用户单击“查看请求”,它将传递实际数字。我的代码如下:

web.php

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 (我省略了大多数冗余代码。以下是与此问题相关的唯一代码)

<!-- 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>

更新:

我注意到,如果我仅使用return redirect()->back();而不是return view('leave.show', ['leavereqs' => Leave::where('leavenr', $leavenr)->first()]);,它将按预期工作。 Laravel是否有可能在缓存app.blade.php文件,并且在重定向到另一个页面时实际上没有刷新它,从而导致计数没有更新?

0 个答案:

没有答案
相关问题