I have four tables: users, orders, shares, and horses.
One user has many orders, one order has many shares, and one horse has many shares.
I want to find how many shares of each horse does a specific user owns (including when a user owns zero shares in a particular horse).
I have tried the following, however I cannot retrieve the cases when a user owns zero shares for a particular horse.
$userHorses = DB::table('orders')->join('shares', 'orders.id', '=', 'shares.order_id')
->join('horses', 'horses.id', '=', 'shares.horse_id')
->where('orders.user_id',$user_id)
->select('horses.name','horses.id','horses.slug',DB::raw('count(*) as totalShares'))
->groupBy('horses.name','horses.id','horses.slug')
->get();
return $userHorses;