Full text search with Laravel Scout and Spatie Laravel Query Builder

Submitted by flix - 1 month ago

Suppose you want to use Laravel query builder to search items in your db like this ?filter[search]=yourkeyword

//your model
/**
 * Scope a query to only include services matching the search term.
 */
public function scopeWhereScout(Builder $query, string $search): Builder
{
    return $query->whereIn(
        'id',
        self::search($search)
            ->get()
            ->pluck('id'),
    );
}

//your controller

$models = QueryBuilder::for(Model::class)
            ->allowedFilters([
                AllowedFilter::scope('search', 'whereScout'),
            ])
            ->get();