提问人:VIQRAM ANANTA WATAF 提问时间:11/11/2023 更新时间:11/11/2023 访问量:34
在控制器空结果中查询雄辩,为什么?
query eloquent in controller null result, why?
问:
$rooms3 = Room::join("notifications", "notifications.room_id", "rooms.id")
->where('rooms.user_id', Auth::user()->id)
->join("watt_histories", "watt_histories.notification_id", "notifications.id")
->where('notifications.id', 'watt_histories.notification_id')
->latest('watt_histories.created_at')
->first();
如果查询运行良好,则给出的返回值不为 null
答:
0赞
Karl Hill
11/11/2023
#1
如果要比较表之间的 ID,则可能是 where('notifications.id', '=', 'watt_histories.notification_id') 或类似,假设 notifications.id 和 watt_histories.notification_id 是需要匹配才能成功查询的列。
$rooms3 = Room::join("notifications", "notifications.room_id", "rooms.id")
->where('rooms.user_id', Auth::user()->id)
->join("watt_histories", "watt_histories.notification_id", "notifications.id")
->where('notifications.id', '=', 'watt_histories.notification_id') // Corrected the comparison here
->latest('watt_histories.created_at')
->first();
0赞
patricus
11/11/2023
#2
您需要删除 .原因有二:->where('notifications.id', 'watt_histories.notification_id')
- 此条件已由您指定的联接处理。
- 该方法用于将列与提供的值进行比较。在本例中,您将该列与 的文本字符串进行比较,这就是您没有得到任何结果的原因。若要比较两列,请使用该方法。但是,如上文 1 所述,不需要此条件,因为它已在联接中指定。
where()
notifications.id
"watt_histories.notification_id"
whereColumn()
$rooms3 = Room::join("notifications", "notifications.room_id", "rooms.id")
->where('rooms.user_id', Auth::user()->id)
->join("watt_histories", "watt_histories.notification_id", "notifications.id")
->latest('watt_histories.created_at')
->first();
评论
dd($room3->toSql());
first()