Answer the question
In order to leave comments, you need to log in
How to display ads pinned to the TOP on Laravel?
There is a bulletin board where users can pay for the service "Pin in the TOP" and the ad will be assigned to one of the three positions in the TOP if it falls under this search. Can I somehow solve this with one request? In the sense of two requests, it’s clear how to do it, but is it possible to sort the TOP 3 ads at the top within one request, and the rest (even if they were previously in the TOP, just leave them in the search results).
Answer the question
In order to leave comments, you need to log in
Query followed by filtering. If you need to choose from these TOP 3 random ads. Because MySQL (if you use it) works very hard with random sampling.
And then decide for yourself whether you make a request 1 with TOP and not top, and then filter it, or make 2 requests TOP + not top. And among the TOP filter the ones you need. I would choose 2 requests. Since it is not clear how many records the script will have to filter. Yet MySQL can win here in terms of speed.
Are you a masochist? Why abbreviate the irreducible. 1 or 2 requests is not the same as 1 or 20 requests.
If you know how to sort in 2 steps, then do it, there is no need to make a request in the first place, just for the sake of placing it in one process - this is stupidity.
Well, it all depends on how your TOP is implemented. Maybe just sorting will do.
And how do you implement the label "Top"?
If it's just a flag in the "has_priority" type database, then everything is simple:
And then you combine it with other output and sort conditions.
Here is getting the first three entries from the top
DB::table('articles')->orderBy('has_priority', 'desc')->take(3)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question