salman15's avatar

Order products by categories with the highest count.

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,
        ]);
0 likes
4 replies
Nakov's avatar

So if you do this:

Category::with(['products' => function ($query) {
            $query;
        }, 'products.images', 'products.categories'])
            ->withCount('products')
			->orderBy('products_count', 'desc')
            ->get();

or

->orderByDesc('products_count')
1 like
salman15's avatar

@Nakov Sorry, I should have made this clear I'm trying to return only the products paginated

salman15's avatar

@Nakov This worked with some adjustments to replicate the paginate function on queries

Please or to participate in this conversation.