I have a table called products and variants. They have one to many relationship, product can have multiple variants. The table ‘products’ doesn’t have a price column, that column is within the variants table. The product’s price is represented by the lowest price of its variants in the dashboard.
In the dashboard, I need to make a function that sorts the product’s price from lowest to highest and paginates it. Due to the nature of Eloquent builder and my intentional database design, it’s not that simple. Because I have to use model methods in order to access the lowest price of the variants like this:
protected function price(): Attribute
{
return new Attribute(get: function(){
return $this->variants()->get()->min('price');
});
}
I’ve tried a couple of things like using sortBy but that’s only for collection so I can’t paginate it, even if I can, that would be a huge memory loss if the query is big enough because of the nature of collection.
Summary
How can I sort the data by a custom model method (price) in my builder before paginating it?