Multi tenancy - Select relationship() - Searchable() remove tenant information when clicked

Forms\Components\Select::make('client_id')
->relationship('client', 'name')
->required()
->searchable()
->preload()
->noSearchResultsMessage(__('No client found'))
->createOptionForm([
Forms\Components\TextInput::make('name')
->required(),
Forms\Components\TextInput::make('phone')
->tel(),
Forms\Components\TextInput::make('email')
->email(),
]),
Forms\Components\Select::make('client_id')
->relationship('client', 'name')
->required()
->searchable()
->preload()
->noSearchResultsMessage(__('No client found'))
->createOptionForm([
Forms\Components\TextInput::make('name')
->required(),
Forms\Components\TextInput::make('phone')
->tel(),
Forms\Components\TextInput::make('email')
->email(),
]),
When I load the page, this query is run
select `clients`.`name`, `clients`.`id` from `clients` where `clients`.`company_id` in (1) order by `clients`.`name` asc
select `clients`.`name`, `clients`.`id` from `clients` where `clients`.`company_id` in (1) order by `clients`.`name` asc
But as soon as I click the select-input, it runs this query and load all the clients
select `clients`.`name`, `clients`.`id` from `clients` order by `clients`.`name` asc
select `clients`.`name`, `clients`.`id` from `clients` order by `clients`.`name` asc
The same happens when I click the "create" button. Is the only way to use options(), and remove the option to create a new client from that page? Or have I missed something in the docs?
Forms\Components\Select::make('client_id')
// ->relationship('client', 'name')
->options(Client::all()->pluck('name','id')) // Custom Middleware add whereBelongs()
->required()
->searchable()
->preload()
->noSearchResultsMessage(__('No client found')),
Forms\Components\Select::make('client_id')
// ->relationship('client', 'name')
->options(Client::all()->pluck('name','id')) // Custom Middleware add whereBelongs()
->required()
->searchable()
->preload()
->noSearchResultsMessage(__('No client found')),
On the last example, this query is run everytime:
select * from `clients` where `clients`.`company_id` in (1)
select * from `clients` where `clients`.`company_id` in (1)
Solution:
From the docs:
Form component and filter scoping. When using the Select, CheckboxList or Repeater form components, the SelectFilter, or any other similar Filament component which is able to automatically fetch "options" or other data from the database (usually using a relationship() method), this data is not scoped. The main reason for this is that these features often don't belong to the Filament Panel Builder package, and have no knowledge that they are being used within that context, and that a tenant even exists. And even if they did have access to the tenant, there is nowhere for the tenant relationship configuration to live. To scope these components, you need to pass in a query function that scopes the query to the current tenant.
`use Filament\Facades\Filament;...
Jump to solution
2 Replies
Solution
Ben
Ben15mo ago
From the docs:
Form component and filter scoping. When using the Select, CheckboxList or Repeater form components, the SelectFilter, or any other similar Filament component which is able to automatically fetch "options" or other data from the database (usually using a relationship() method), this data is not scoped. The main reason for this is that these features often don't belong to the Filament Panel Builder package, and have no knowledge that they are being used within that context, and that a tenant even exists. And even if they did have access to the tenant, there is nowhere for the tenant relationship configuration to live. To scope these components, you need to pass in a query function that scopes the query to the current tenant.
use Filament\Facades\Filament; use Filament\Forms\Components\Select; use Illuminate\Database\Eloquent\Builder; Select::make('author_id') ->relationship( name: 'author', titleAttribute: 'name', modifyQueryUsing: fn (Builder $query) => $query->whereBelongsTo(Filament::getTenant())), );
Want results from more Discord servers?
Add your server