The aim is to join 3 tables, so far I succeeded with two, but the third table query requires calculations to be made, and I'm stuck with DB::raw and select... So far I have this:
$products = DB::table('products')
->join('users', 'username', '=', 'products.owner_id')
->join('likeable_like_counters', 'likeable_id', '=', 'products.id')
->join('user_reviews', 'user_id', '=', 'users.id')
->select([
'products.*',
'likeable_like_counters.count AS productLikeCount',
'users.image AS userAvatar',
'users.id AS userID',
])
->get();
$userRating = DB::table('user_reviews')
->select(DB::raw('SUM(rating) / count(rating) as userRating, user_id'))
->groupBy('user_id')
->get();
I would like to have $userRating query within $products.
I tried:
$products = DB::table('products')
->join('users', 'username', '=', 'products.owner_id')
->join('likeable_like_counters', 'likeable_id', '=', 'products.id')
->join('user_reviews', 'user_id', '=', 'users.id')
->select([
'products.*',
'likeable_like_counters.count AS productLikeCount',
'users.image AS userAvatar',
'users.id AS userID',
])
->select(DB::raw('SUM(user_reviews.rating) / count(user_reviews.rating) as userRating, user_id'))
->groupBy('user_id')
->get();
but then it just gives me the userRating and user_id. Am I using syntax wrong? or is it impossible?