Skip to content
Advertisement

can’t use paginate when use sortby in Laravel

I Want to get products and sort it by mutator attribute in my case it’s called price

i search for how to sort it and found that i can use sortBy from the collection

like that

JavaScript

it works fine, but when I try to add paginate nothing happens and there is no paginate in the response

JavaScript

So how can i add paginate in this case?

Edit

price mutator

JavaScript

Advertisement

Answer

Unfortunately there is no way to call paginate() on collection. You may manually paginate the collection result or modify your query and use addSelect or orderByRaw to include mutated value:

JavaScript

By the way, I think you’ll get the same result to order by sell_price column instead of price mutated value if your discount is a constant value, so try orderBy('sell_price') instead of sortBy('price'):

JavaScript

Laravel Docs: https://laravel.com/docs/master/queries#ordering-grouping-limit-and-offset

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement