I have a table places and table rating. Every place have multiple rows in rating table. I want to get all places and every place object should have one property called user_rating. Its value is the average of rating.rate_no for this place.If there is no ratings for this place (no rows in rating table) user_rating should be 0 My code is:
$data = DB::table('places')
->join('rating', 'places.id', '=', 'rating.place_id', 'left outer')
->select(
'places.*',
'rating.rate_no',
DB::raw('avg(rating.rate_no) as user_rating')
)
->groupBy('rating.rate_no')
->get();
This code group all places in one object with avarage(rating) which I do not want.