Rediska's avatar

How to get all data if pagination is used?

I can't seem to get the job done when I use pagination.

This is how I receive products:

 $products = Product::getProducts($categories, $attributes, $sortName, $sortOrder);

This is what I put in the model:

public static function getProductsWithAttributes ($categories, $attributes, $sortName, $sortOrder) {
        return static::select()
            ->whereIn('category_title', $categories)
            ->where(
                'stock', '1'
            )
            ->where(function ($query) use ($attributes) {
                foreach ($attributes as $attributeName => $names) {
                    $query->where(function ($query) use ($attributeName, $names) {
                        $query->whereIn($attributeName, $names);
                    });
                }
            })
            ->orderBy($sortName, $sortOrder)
            ->paginate(40);
    }

Products have different attributes - price, old price, color, season, etc.

So I only get 40 entries out of eg 1000 entries. But how can I get the lowest price for all items? Or take all the values of the color attribute without repetition? In my case, the search is only for the first 40 records.

I didn't find a better solution than to make another request:

$minPrice = Product::getMinPrice($categories, $attributes);
            dump($minPrice->price);
public static function getMinPrice ($categories, $attributes) {
        return static::select('price')
            ->whereIn('category_title', $categories)
            ->where(
                'stock', '1'
            )
            ->where(function ($query) use ($attributes) {
                foreach ($attributes as $attributeName => $names) {
                    $query->where(function ($query) use ($attributeName, $names) {
                        $query->whereIn($attributeName, $names);
                    });
                }
            })
            ->orderBy('price', 'asc')
            ->first();
    }

But it seems to me it is unreasonable to access the database every time.

0 likes
7 replies
vincent15000's avatar

Are you using a package ? Or are all these functions your own functions ?

Rediska's avatar

@vincent15000 Then there will be a very long request. I have quite a lot of products in the database. Pagination paginates and loads the request faster.

1 like
vincent15000's avatar

@Rediska But how can I get the lowest price for all items?

Sorry I read too fast. The lowest price for all items, I read only all items ;).

For the lowest price, you don't need to paginate, you will retrieve only one product : the one with the lowest price. So a get() or first() function is a good idea.

Rediska's avatar

@vincent15000 It turns out that you need several database accesses? One request with paination to receive products. The second request is to get the minimum and maximum prices. And then there will be a third and fourth request to get all the available attributes to display from in the product filter? =))

1 like
vincent15000's avatar

@Rediska That's the idea. The best pratice is that each method manages only one logic.

If you need to retrieve paginated datas, it will return a collection of models.

If you need to retrieve a maximum or a minimum value, that's not the same request, so you need to manage it via another method.

Your questions let me think that you should follow some Laravel series on Laracast.

https://laracasts.com/series/laravel-8-from-scratch

This very good tutorial will give you a lot of good ideas to have a good structured code.

Please or to participate in this conversation.