I
I
Igor T2020-07-17 09:13:07
Laravel
Igor T, 2020-07-17 09:13:07

How to set sorting in Laravel query?

Friends!
Help me to understand! You need to select goods from the database, sorted in the following order and priority:
1. First, those that are available with any balance , i.e. without sorting them by a specific quantity ("quantity" field)
2. By the number of orders in descending order ("ordercount" field)
3. By the number of views in descending order ("browsecount" field)
I have a problem in the first paragraph. I sort by the rest, but I need goods with a balance > 0 to come forward, and sort them by p.p. 2-3
There should also be pagination. Therefore, I understand that it is a bad idea to collect products in two requests (first those that are in stock, then the rest)
Here is my request:

$products = Product::whereCategory_id($category->id)->orderBy('quantity','desc')
->orderBy('ordercount','desc')->orderBy('browsecount','desc')->paginate(12);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Anton, 2020-07-17
@igor--ok

TryorderBy(DB::raw('quantity > 0'),'desc')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question