Troubleshooting $search variable accessibility in Select Field modifyQueryUsing() method

According to the document, the $search variable should be accessible within https://filamentphp.com/docs/3.x/forms/fields/select#customizing-the-relationship-query
Customizing the relationship query You may customize the database query that retrieves options using the third parameter of the relationship() method:
use Filament\Forms\Components\Select;
use Illuminate\Database\Eloquent\Builder;

Select::make('author_id')
->relationship(
name: 'author',
titleAttribute: 'name',
modifyQueryUsing: fn (Builder $query) => $query->withTrashed(),
)
use Filament\Forms\Components\Select;
use Illuminate\Database\Eloquent\Builder;

Select::make('author_id')
->relationship(
name: 'author',
titleAttribute: 'name',
modifyQueryUsing: fn (Builder $query) => $query->withTrashed(),
)
If you would like to access the current search query in the modifyQueryUsing function, you can inject $search.
I tried adding $search in the modifyQueryUsing but i got an error
Select::make('skills')
->multiple()
->relationship(
titleAttribute: 'name',
modifyQueryUsing: function (Builder $query, ?string $search) {
return $query->withTrashed()
->where('name', 'LIKE', '%' . $search . '%')
->orderByRaw(
"CASE
WHEN name LIKE ? THEN 1
WHEN name LIKE ? THEN 2
ELSE 3
END, name ASC",
[$search . '%', '%' . $search . '%']
)
->limit(20);
}
)
Select::make('skills')
->multiple()
->relationship(
titleAttribute: 'name',
modifyQueryUsing: function (Builder $query, ?string $search) {
return $query->withTrashed()
->where('name', 'LIKE', '%' . $search . '%')
->orderByRaw(
"CASE
WHEN name LIKE ? THEN 1
WHEN name LIKE ? THEN 2
ELSE 3
END, name ASC",
[$search . '%', '%' . $search . '%']
)
->limit(20);
}
)
An attempt was made to evaluate a closure for [Filament\Forms\Components\Select], but [$search] was unresolvable.
$search parameter should be accessible inside the modifyQueryUsing() method, right? am i missing something or is this a bug?
0 Replies
No replies yetBe the first to reply to this messageJoin