Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 如何将通知标记返回为已读_Laravel_Notifications - Fatal编程技术网

Laravel 如何将通知标记返回为已读

Laravel 如何将通知标记返回为已读,laravel,notifications,Laravel,Notifications,我想在检查时将通知标记为已读,我尝试将它们设置为已读,但它不起作用 这是我的app.blade.php <li class="dropdown" id="markasread" onclick="markNaotificationAsAread()"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expand

我想在检查时将通知标记为已读,我尝试将它们设置为已读,但它不起作用 这是我的app.blade.php

 <li class="dropdown" id="markasread" onclick="markNaotificationAsAread()">

                            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="true">
            <span class="glyphicon glyphicon-globe"></span> Notifications <span class="badge ">{{count(Auth()->user()->unreadNotifications)}}</span>

                            </a>
                            <ul class="dropdown-menu" role="menu">

                                    @foreach(Auth()->user()->unreadNotifications as $notification)
                                 <li>
                                    @if(isset($notification->data['ad_object']))
        <a href="#">{{$notification->data['user_name']}} posted a new ad  "{{$notification->data['ad_object']}}"</a>
                           @endif

                            @if(isset($notification->data['comment_content']))
        <a href="#">{{$notification->data['user_name']}} posted a new comment "{{$notification->data['comment_content']}}"</a>
                           @endif
                                 </li>
                                 @endforeach



                            </ul>
                        </li>

                        <li class="dropdown">
})


我能做什么请帮助我

关于如何将通知标记为已读,您的代码留下了太多的想象,但最简单的方法是在通知表中有一列“read”数据类型Boolean/tiny int

创建通知时,读取列的默认值为false

然后,当单击通知时,您可以将用户路由到一个简单的方法,该方法将通知表中的读取列更新为true。然后将用户重定向到通知将他们带到的任何位置

Route::get('/markAsRead/{notification}',function($notification ){
    $mark = Auth()->user()->unreadNotifications->where 
    ('id', $notification)->first ();

    $mark->update (['read' => true]);

   return redirect ()->route('routename');
});
当用户单击某个通知时,您可以将其用于单个通知。或者,如果出于某种原因想将所有通知都标记为已读,那么在对结果进行迭代时,您也会做同样的事情


不过,您应该将所有逻辑移到控制器上

你可以花一些时间格式化这个问题,这样人们就不会头疼了。Auth->user->unreadNotifications可能会返回一组通知对象。您需要在集合中循环并更新每个单独的itemnotification。
Route::get('/markAsRead/{notification}',function($notification ){
    $mark = Auth()->user()->unreadNotifications->where 
    ('id', $notification)->first ();

    $mark->update (['read' => true]);

   return redirect ()->route('routename');
});