skip to Main Content
$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


  1. 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.

    $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();
    
    Login or Signup to reply.
  2. You need to remove ->where('notifications.id', 'watt_histories.notification_id'). Two reasons:

    1. This condition is already handled by the join you specified.
    2. The where() method is used to compare a column to a provided value. In this case, you’re comparing the column notifications.id to the literal string of "watt_histories.notification_id", which is why you’re not getting any results. To compare two columns, use the whereColumn() method. However, as mentioned in 1 above, this condition isn’t needed since it’s already specified in the join.
    $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();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search