laravel-scout-elasticsearch: eager load is not working

Software Version PHP 7.2.5 Elasticsearch 7.9.3 Laravel 7 Laravel Scout 8.3

I followed the steps from the eager load section but the method from in the MyImportSourceFactory is not triggered

Should we call that method manually ? Or it is supposed to be triggered automatically ?

Also I saw that now there is a method named makeAllSearchableUsing in the Searchable class which can be used to eager load relationships. Is that supported from the package ?

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 1
  • Comments: 26 (24 by maintainers)

Most upvoted comments

@arturslogins just stumbled upon this issue, but it’s actually very easy to use Scout’s native makeAllSearchableUsing method in this package

namespace App\Providers;

use Matchish\ScoutElasticSearch\Searchable\ImportSourceFactory;

class AppServiceProvider
{
    public function register(): void
    {
        $this->app->bind(ImportSourceFactory::class, MyImportSourceFactory::class);
    }
}
namespace App\Services\ElasticSearch;

use Matchish\ScoutElasticSearch\Searchable\DefaultImportSource;
use Matchish\ScoutElasticSearch\Searchable\ImportSource;
use Matchish\ScoutElasticSearch\Searchable\ImportSourceFactory;

final class MyImportSourceFactory implements ImportSourceFactory
{
    public static function from(string $className): ImportSource
    {
        return new DefaultImportSource($className, [new ModelSearchableQueryScope()]);
    }
}
class ModelSearchableQueryScope implements Scope
{
    public function apply(Builder $builder, Model $model): void
    {
        if (in_array('makeAllSearchableUsing', get_class_methods($model), true)) {
            $model->makeAllSearchableUsing($builder);
        };
    }
}

The only thing to be aware of it that the visibility of the method must be set to public (unless you want to use Reflection)

class MyModel extends Model
{
    public function makeAllSearchableUsing($query)
    {
        return $query->with('relationship');
    }
}

makeAllSearchableUsing is working if you run makeAllSearchable but in the package command it ignored for now. Probably it make sense to use it in DefaultImportSource. Thanks for mentioned it. I’ve missed this new api

Hello, when you plan to include makeAllSearchableUsing method ?