$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();
If the query runs well, the return given is not null
2
Answers
If you meant to compare IDs between the tables, it should likely be where(‘notifications.id’, ‘=’, ‘watt_histories.notification_id’) or similar, assuming that notifications.id and watt_histories.notification_id are columns that need to match for the query to be successful.
You need to remove
->where('notifications.id', 'watt_histories.notification_id')
. Two reasons:where()
method is used to compare a column to a provided value. In this case, you’re comparing the columnnotifications.id
to the literal string of"watt_histories.notification_id"
, which is why you’re not getting any results. To compare two columns, use thewhereColumn()
method. However, as mentioned in 1 above, this condition isn’t needed since it’s already specified in the join.