F
F
freeeeez2017-09-21 16:28:54
Laravel
freeeeez, 2017-09-21 16:28:54

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

5 answer(s)
S
Stanislav Pochepko, 2017-09-21
@freeeeez

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.

E
Evgeny Antipov, 2017-09-21
@elevennine

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.

K
Konstantin B., 2017-09-21
@Kostik_1993

Well, it all depends on how your TOP is implemented. Maybe just sorting will do.

A
Alexey Budaev, 2017-09-21
@Markus_Kane

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.

T
Twelfth Doctor, 2017-09-21
@verdex

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 question

Ask a Question

731 491 924 answers to any question