I'm try to order my products by categories with the highest count.
So for example, if there are 3 categories which have products assigned to them like here:
Electronics: 3
Clothing: 6
Foods: 2
I would like the products with the category clothing to appear at the start of my result, followed by electronics and ended by foods.
Currently I have:
$categories = Category::with(['products' => function ($query) {
$query;
}, 'products.images', 'products.categories'])
->withCount('products')
->get();
$jobs = new \Illuminate\Database\Eloquent\Collection;
$categories->map(function ($category) use ($products) {
$category->products->map(function ($product) use ($products) {
$products->merge($product);
});
});
return $products->all();
Which returns what I want however I can't use pagination with this.
How can I order/sort this way with pagination?
I'm trying to return the products in a collection like this:
return ProductResource::collection($products->orderBy('id', 'DESC')
->paginate(24))->additional([
'warnings' => $warnings,
]);