Friday, March 31, 2017

Laravel query builder strange result with join

I have a query

$orders = DB::table('orders')->where('user_id', $user->id)->get();  

And this is what I have in view:

@foreach ($orders as $order)
   
@endforeach

It prints out 1 2 3, because the table has these three IDs. But if I try to join, I get a rather unpredicted result.

$orders = DB::table('orders')->where('user_id', $user->id)->
leftJoin('status', 'orders.status_id', '=', 'status.id')
->get();

It gives 2 1 1. Result is the same with rightJoin() and join().

I thought this command would append row from status table to every corresponding row of orders table. Join on orders.status_id = status.id. Can I get an intended result?



via TheKitMurkit

Advertisement