One solution to this issue is to use the chunkById method to retrieve the models in batches and then eager load the relationships before indexing them. Here's an example implementation:
protected function makeAllSearchableUsing($query)
{
return $query->with('brands.brand');
}
public function queueMakeAllSearchable()
{
$self = $this;
$this->newQuery()->chunkById(100, function ($models) use ($self) {
$models->load('brands.brand');
$self->makeAllSearchable($models);
});
}
In this example, we're using the chunkById method to retrieve the models in batches of 100. We then load the relationships for each batch of models before passing them to the makeAllSearchable method to be indexed.
To queue this job, you can add a new method to your model that calls the makeAllSearchable method in a queued job:
public function queueMakeAllSearchable()
{
$this->with('brands.brand')->searchable();
}
Then, in your code, you can call this method to queue the job:
$model->queueMakeAllSearchable();
This will enqueue a job to index all of the models in the background using the SCOUT_QUEUE setting.