If you want to create a dedicated scout builder class for a model, you currently have to override the static search() method.
// Before
class Company extends Model
{
use Searchable;
public static function search($query = '', $callback = null)
{
return app(CompanyScoutBuilder::class, [
'model' => new static,
'query' => $query,
'callback' => $callback,
'softDelete' => static::usesSoftDelete() && config('scout.soft_delete', false),
]);
}
}
This PR fixes that so you only have to define a static property on your model
// After
class Company extends Model
{
use Searchable;
protected static string $scoutBuilder = CompanyScoutBuilder::class;
}
class CompanyScoutBuilder extends \Laravel\Scout\Builder
{
public function withinRadius() : self
{
// Some complex filter that you want to use in multiple scout searches.
// Creating a method in a custom scout builder class makes this easy.
return $this->where(...);
}
}
This is similar to the static $builder and $collectionClass properties on a model. Those properties respectively allow you to set a custom Eloquent Builder class and an Eloquent Collection class.
If you want to create a dedicated scout builder class for a model, you currently have to override the static
search()
method.This PR fixes that so you only have to define a static property on your model
This is similar to the static
$builder
and$collectionClass
properties on a model. Those properties respectively allow you to set a custom Eloquent Builder class and an Eloquent Collection class.