php - 在 Laravel 5.3 通知中将特定通知标记为已读

标签 php laravel laravel-5

我知道有很多方法可以像这样在 L5.3 中标记为已读用户的所有通知:

$user = App\User::find(1);

foreach ($user->unreadNotifications as $notification) {
    $notification->markAsRead();
}

或者:

$user->unreadNotifications->markAsRead();

但假设我想将具有给定 ID 的特定通知标记为已读。

其实我已经做了一个这样的未读用户通知列表:

<ul class="menu">
        @foreach($AuthUser->unreadNotifications as $notif)
            <li>
                <a href="{{$notif->data['action']}}" data-notif-id="{{$notif->id}}">
                    {{$notif->data['message']}}
                </a>
            </li>
        @endforeach
</ul>

如您所见,每个 a 标签都有一个包含通知 ID 的 data-notif-id 属性。

现在我想通过 Ajax(在单击事件上)将此 ID 发送到脚本,并将该通知标记为已读。为此我写了这个:

$('a[data-notif-id]').click(function () {

        var notif_id   = $(this).data('notifId');
        var targetHref = $(this).data('href');

        $.post('/NotifMarkAsRead', {'notif_id': notif_id}, function (data) {
            data.success ? (window.location.href = targetHref) : false;
        }, 'json');

        return false;
});

NotifMarkAsRead 指的是下面的 Controller :

class NotificationController extends Controller
    {
        public function MarkAsRead (Request $request)
        {
              //What Do I do Here........
        }
    }

在没有任何Notification 模型的情况下我该怎么做?

最佳答案

根据 this Answer在 Github 上,解决方案是:

Illuminate\Notifications\DatabaseNotification is where the Model for the notifications exists, you can use it to grab a notification by ID and delete it. Also if you don't want to use the model you can use a normal DB query.

关于php - 在 Laravel 5.3 通知中将特定通知标记为已读,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39329414/

相关文章:

javascript - 如何将 JSON 与 react-ace 一起使用?

php - 无法在 Jquery/Ajax 中调用 Laravel 路由

php - 错误: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'rowid' in 'where clause'

php - 将自定义总计行添加到 WooCommerce 管理订单总计

php - 缩短代码、ORDER BY 和 SWITCH

php - CodeIgniter 多数据库持久连接?

php - Laravel 5 命名空间问题

php - 用户应该成为 Laravel 中的资源吗?

php - Laravel 5 加密 - 给定相同字符串的不同值?

mysql - Laravel 路由参数未修剪(通常在添加空格时有效)