You can sort by date then rank, or by rank then date .... one or the other...
Laravel Eloquent sort by date but also by rank index to hold up entries
I am building a project with Laravel and I have a table which contains all my products. On that table, products get added daily, and I show all products on my page sorted by created_at. That's a pretty easy job to do with Laravel Eloquent and ->orderBy('created_at', 'DESC').
However, I want to have the possibility to "hold up"/"pin" certain products to a certain place. For that, I have created the column rank_index which contains the number the product should have in the returned query collection.
This is my current table:
title rank_index created_at
An awesome product 2023-01-01 10:04:00
Another product 4 2023-01-01 10:00:00
Baby car 2023-01-01 10:05:00
Green carpet 2 2023-01-01 10:08:00
Toy 2023-01-01 10:07:00
And the following table shows the collection I want my query to return:
title rank_index created_at
Toy 2023-01-01 10:07:00
Green carpet 2 2023-01-01 10:08:00
Baby car 2023-01-01 10:05:00
Another product 4 2023-01-01 10:00:00
An awesome product 2023-01-01 10:04:00
Preferably, I would like to have a solution which directly returns me the table like this from the database. This way I don't have to split and slice the collection, which makes the request much slower! Otherwise, I have to rearrange, split and slice the collection with PHP functions.
I am happy about any help!
Kind regards
Please or to participate in this conversation.