Laravel Notifications将每个标记标记为已读

时间:2017-09-21 10:22:52

标签: php laravel laravel-5.4

如何将用户的每个通知标记为已读,目前我将所有通知标记为如下所示:

路线

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

控制器

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

        return back();
    }

表格

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

2 个答案:

答案 0 :(得分:0)

如果您的表单中有这样的内容:

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

您需要做的就是在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();
}

答案 1 :(得分:0)

尝试:

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

    return back();
}