No ideas, guys?
I believe that more people have been faced (or facing) it as having a searchable and sortable grid is a common need. Thanks?
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello. This is my first post here and have to say I am glad to join the Laracasts community! Let's go, then!
I am using Laravel/Scout/Elasticsearch via https://packagist.org/packages/babenkoivan/scout-elasticsearch-driver and I have this situation:
$visits = Visit::search("canada")->with(['origin', 'profile', 'user'])->paginate(10);
It works very well.`
As you can see, this model is related to three other tables: origins (that are countries), profile and users. The way everything is set, I can search for "canada" and get all the visits that came from Canada, what is fine as well.
The result is shown in a grid using paginate(), what works well, too.
However, if I want to sort the grid by Origins -- for everything I read and tried so far -- I can't use the $model::search('canada')->with(...) approach. Instead, I have to use something like
$visits = DB::table('visits')
->join('origins', 'origins.id', 'visits.origin_id')
->orderby($sort_column, $sort_az_za)
->paginate(10);
This is the thing: the search() will not work with the DB::table('visits') syntax and Visit::search("canada")->with(....)... approach does not allow me to sort by a field that is not in the Visits table. BTW,
Visit::search("$query")->join('origins', 'origins.id', 'visits.origin_id')...
will not work as well.
The question is: how can I search and sort the results by any field I want?
I thought that it should be possible to extend Scout to search DB::table('TABLE') or to try to sort directly via ES -- though I would prefer to keep all the code bound to the "Laravel way".
If extending the DB::table('TABLE') is a way, could someone point me where to start?
Other than that, any other ideas/insights?
Thanks!
Please or to participate in this conversation.