php - 将单个 Laravel 通知标记为已读
问题描述
我的应用程序中有一个页面,其中显示了用户当前拥有的所有未读通知的列表,并且每个通知都有一个已读图标。
数据库中的 ID 设置为char
一长串字母和数字。当我在页面上显示通知的 ID 时,它们都返回与数据库中的内容毫无关系的数字,因此当我运行查询以读取通知时,它找不到 ID,因为 ID 不是t 匹配。
在 Laravel 中读取单个通知的最佳方法是什么?我正在使用下面的代码进行尝试,但这$request->get('id')
是不正确的,因为我似乎将 12310 作为 ID,其中一些甚至是 0。
Auth::user()->unreadNotifications->where('id', $request->get('id'))->markAsRead()
解决方案
Laravelnotifications
表id
用作CHAR
默认字段类型。因此,当您过滤某些 id 时,您必须使用first()
如下方式。由于unreadNotifications
是Illuminate\Notifications\DatabaseNotificationCollection
$notificationId = request('notification_id');
$userUnreadNotification = auth()->user()
->unreadNotifications
->where('id', $notificationId)
->first();
if($userUnreadNotification) {
$userUnreadNotification->markAsRead();
}
推荐阅读
- salesforce - 在目标工具中按外部 ID 映射 Salesforce 查找字段
- javascript - Discord js直接消息等待不起作用
- oracle - 当插入期间日期值为空时,JPA @query 不起作用
- azure-pipelines - 是否可以更改 Azure Pipelines 与自托管代理一起使用的本地子文件夹?
- http-post - Sipgate 推送 API
- magento2 - Magento 2如何使用如果父类别锚定从类别列表页面隐藏子类别产品
- python - 我不完全理解这个脚本中的 any() 函数是如何工作的
- r - 突然连接到redshift dbConnect时出错
- javascript - 如何在 discord.js 中为命令处理程序添加别名
- python - Django 自定义 PasswordResetView:__init__() 缺少 1 个必需的位置参数:'user'