Multi tenancy - Select relationship() - Searchable() remove tenant information when clicked
When I load the page, this query is run
But as soon as I click the select-input, it runs this query and load all the clients
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?
On the last example, this query is run everytime:
Solution:Jump to 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;...
2 Replies
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;
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())),
);