Level 25
Productcategory::select(DB::raw('count(category_id) as categoryCount','category_id','product_id')
->groubBy('category_id','product_id')
->orderBy('categorycount','desc')
->get();
1 like
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I want to use laravel eloquent to implement the below query. Please help
SELECT COUNT(`category_id`) as categorycount,`category_id`,`product_id` FROM `productcategories` GROUP BY `category_id` ORDER BY categorycount DESC
Model Name is Productcategory
Try this-
App\Productcategory::selectRaw('category_id, product_id, count(*) categorycount')
->orderBy('categorycount', 'desc')
->groupBy('category_id')
->get();
Please or to participate in this conversation.