I am using Laravel/Scout/Elasticsearch via https://packagist.org/packages/babenkoivan/scout-elasticsearch-driver and I have this situation:
-
To search, I must use a model like this:
$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 the 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->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 andVisit::search("canada")->with(....)...
approach does not allow me to sort by a field that is not in the Visits table.
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?