© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
FilamentF
Filament•2y ago•
1 reply
manojhl 🇮🇳

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()
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
modifyQueryUsing
function, you can inject
$search
$search
.

I tried adding $search in the
modifyQueryUsing
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
$search
parameter should be accessible inside the
modifyQueryUsing()
modifyQueryUsing()
method, right? am i missing something or is this a bug?
Select - Form Builder - Filament
Filament banner
FilamentJoin
A powerful open source UI framework for Laravel • Build and ship admin panels & apps fast with Livewire
20,307Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Search modified select field
FilamentFFilament / ❓┊help
9mo ago
Select field - Multiple & Search
FilamentFFilament / ❓┊help
3y ago
Select field with modifyQueryUsing doesn't validate client input
FilamentFFilament / ❓┊help
3y ago
Select field using search populate another field.
FilamentFFilament / ❓┊help
2y ago