在控制器中查询雄辩结果为空,为什么?

问题描述 投票:0回答:1
$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();

如果查询运行良好,则返回的值不为空

php sql mysql laravel phpmyadmin
1个回答
0
投票

如果您打算比较表之间的 ID,则可能应该是 where('notifications.id', '=', 'watt_histories.notification_id') 或类似的,假设 notification.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();
© www.soinside.com 2019 - 2024. All rights reserved.