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

EckyEckyPtang's avatar

Sort results with Laravel Scout results?

On the Scout docs page https://laravel.com/docs/8.x/scout there's no mention of how to sort the results. When doing

$stories = Story::search($keyword)->orderBy('created_at', 'desc')->take(5)->get();

It ignores the orderBy. Does anyone know why this is happening? I'm using Meilisearch.

0 likes
1 reply
talel's avatar

Search engines such as Meilisearch sorting by relevance and therefore it is not possible to sort on search time. If you do want to use orderBy() on search time you should refer to the Meilisearch documentation (#Configuring MeiliSearch for sorting at search time) and add the fields you are looking to sort by to the sortableAttributes on the configuration file. I have not used it or tested it, let us know.

If not, you can always sortBy() the collection getting from the search engine.

Please or to participate in this conversation.