简体   繁体   English

Laravel 通知将每个标记为已读

[英]Laravel Notifications mark each as read

How can I mark each notification for a user as read, currently I do mark all as read like this:如何将用户的每个通知标记为已读,目前我将所有通知都标记为已读,如下所示:

Route路线

Route::group(['prefix' => 'notification', 'as' => 'notification.'], function () {
    Route::delete('destroy/{userID}}', ['as' => 'destroy', 'uses' => 'NotificationController@destroy']);
});

Controller控制器

    public function destroy($userID)
    {
        $user = User::findOrFail($userID);
        $user->unreadNotifications->map(function($n) {
            $n->markAsRead();
        });

        return back();
    }

Form表格

<form method="post" action="notification/destroy/{{ Auth::id() }}">
    {{ method_field('DELETE') }}
    {{ csrf_field() }}
    <button type="submit">Mark as read.</button>
</form>

If you have in your form some thing like this :如果你的表格中有这样的东西:

<form method="post" action="notification/destroy/{{ Auth::id() }}">
    {{ method_field('DELETE') }}
    {{ csrf_field() }}
    <input type="hidden" name="notification_uuid" value="{{ $notification->id }}">
    <button type="submit">Mark as read.</button>
</form>

All you need to do is add a test in the map method like this :您需要做的就是在 map 方法中添加一个测试,如下所示:

public function destroy($userID, , Request $request)
{
    $user = User::findOrFail($userID);
    $user->unreadNotifications->map(function($n) use($request) {
        if($n->id == $request->get('notification_uuid')){
            $n->markAsRead();
        }
    });

    return back();
}

try this :试试这个:

public function destroy($userID)
{
    $user = User::findOrFail($userID);
    $user->unreadNotifications->get()->map(function($n) {
        $n->markAsRead();
    });

    return back();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM