Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

futurefuture's avatar

Add Items To Beginning Of Collection

Hey there,

Lets say I have:

$productsAtBeginning = [1,3,6565,8,541]
$product = Product::paginate();

Does anyone know of an easy way to get the entire paginated query with the ids in the array at the beginning?

Thanks!

0 likes
15 replies
jlrdw's avatar

Instead of collection, just do a query with an order by.

tykus's avatar

Just add a column to promote those records to the top. For example, add a sort_order column and give those records a higher priority that the other records.

$product = Product::orderBy('sort_order')->paginate();
Snapey's avatar

If you want to use pagination, and you don't want these promoted products showing up at the top of every page, you need @tykus suggestion

futurefuture's avatar

@TYKUS - Thanks for that - but the issue I have with that is that the same products will be returned in different orders depending on the query...

for instance /wine and /new-items would both be querying the same product table but would have individual indexes that should be first..

that's why i need an external index or array of $productsAtBeginning for each query.

not sure if that makes sense?

jlrdw's avatar

If you want to use pagination, and you don't want these promoted products showing up at the top of every page, you need @tykus suggestion

Yeah a regular query.

Snapey's avatar

Doesn't make any sense. You can have item ranked 1 in /wine and also another ranked 1 in /new-items.

It does not matter if there are several things with rank #1 unless they are both on the same page and you care which comes first

futurefuture's avatar

@SNAPEY - Right, but if i want the ranking to be different in both...

so 1 in /wine and 3 in /new-items...and maybe 7 in /what's-hot

That's why I was thinking an individual array of top ranked items for each query that could be sorted against...

Pretty specific, I know :/

tykus's avatar

not sure if that makes sense?

It does not.

i need an external index or array of $productsAtBeginning for each query

You want those $productsAtBeginning at the top of every page of results???

futurefuture's avatar

@tykus

I have 13,000 products but for simplicity, let's say I have 20 products and 4 separate rankings for them depending on url.

$products = [1,2,3,4,5,....]

/wine => product ranking would go [2,3,4,5,9....] /whats-new => product ranking would go [4,5,2,6,7,2...] /whats-hot => product ranking would go [9,6,5,4,3,2....]

etc.

Does that make more sense?

jlrdw's avatar

Can't you just show one product with ratings paginated. If user wants to see another product, have a modal lookup table (with search) to select another product.

I doubt anyone will paginate through 13000 products, only products they are interested in viewing. Then only what type they want to see, i.e., red wine. Sorry if I mis-understand.

Also you do not want thousands of products in a collection.

siangboon's avatar

you may add extra column to identify the type of product hence you can group and sort oder them by type, for what new you can sort order by the created_at and for what's hot probably you need to have a lookup table or extra column to identify what considering "hot" the sort order them accordingly.

futurefuture's avatar

For anyway running into this same issue, i got is working using the following, where $t is the array of product Ids that I want to take precedence, and is variable.

$t = '28,1';

$products = Product::orderByRaw('FIELD(products.id,' . $t . ') desc')->paginate();

Thanks for all the help and hope this helps anyone in need :)

Cronix's avatar

@futurefuture I sure hope you trust the source of wherever $t is coming from, since this isn't using parameter binding and open to sql injection.

futurefuture's avatar

@cronix yea sorry, should have mentioned that. $t is maintained internally, but paramater binding is def a good idea here. thanks for the heads up!

Please or to participate in this conversation.