Are you using a package ? Or are all these functions your own functions ?
Jan 6, 2023
7
Level 1
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.
Please or to participate in this conversation.