Algolia Laravel Scout complex where clauses and eager loading

294 views Asked by At

Since Laravel Scout doesn't support more complex where clauses than simple numeric comparisons.

I checked the source code and I found the following lines

if (!empty($models = $model->getScoutModelsByIds($builder, $modelKeys))) {
                    $instances = $instances->merge($models->load($searchable->getRelations($modelClass)));
                }

The instances is what is returned from Algolia search, so for example the following search essentially returns the $instances variable.

Mode::search('something')->get();

the $model is the searchable model and the getScoutModelsByIds what It basically does is a query to the database like

public function getScoutModelsByIds(){
       $model->whereIn('id', $modelKeys)->get();
}

I was wondering if I apply any kind of where clauses or addSelect, or with eager loading, on the model before actually retrieving the data from the database, is it a good idea ?

For example

$model->where('some condition')->whereIn('id', $modelKeys)->get();

and instead of using lazy loading

$instances = $instances->merge($models->load($searchable->getRelations($modelClass)));

use the with function before retrieving the data from db.

For example

$model->where('some condition')->whereIn('id', $modelKeys)->with('relationships')->get();
0

There are 0 answers