Let’s say I have a method like this:
public function scopeWithLikes(Builder $query) { $query->leftJoinSub( 'select likeable_id, sum(liked) likes, sum(!liked) dislikes from likes group by likeable_id', ); }
My question is, how can I rewrite this query with Eloquent for my Laravel project?
Advertisement
Answer
From what I understood I think this is what you are looking for, try it and tell me:
$orders = DB::table('likes') ->select('likeable_id', DB::raw('SUM(liked) as likes'), DB::raw('SUM(!liked) as dislikes')) ->groupBy('likeable_id') ->get();
You can get more information from the laravel Database documentation Query Builder here: