【发布时间】:2020-08-13 12:36:10
【问题描述】:
我不确定我的标题是否正确,但我想做的是获取用户未阅读的所有通知。 我有 2 个表,第一个表是通知,第二个是 read_notifications。
这是我在 User.php 模型中的代码
$read = DB::table('read_notifications')->select('notification_id')->where('user_id', $this->id)->get();
$unread = Notification::whereNotIn('id', $read)->get();
在这里,我获取了 read_notifications 表中的所有通知 ID,我想将它们放入 $unread 语句中。
我这样做时得到的错误是
stdClass 类的对象无法转换为字符串
【问题讨论】:
-
这能回答你的问题吗? laravel collection to array
-
我确实尝试过,但我仍然遇到同样的错误
-
Notification和ReadNotification之间是否设置了关系?有ReadNotification模型吗?我认为可以使用whereDoesntHave方法通过正确设置关系来完成。