Select form component with relation loads items from other tenants
Hello, I have a form component that loads all of the items from every tenant. How do I only load the items for that tenant?
Select::make('relation_id')->label('Kies een relatie')
->searchable()
->preload()
->required()
->relationship('relation', 'contact_name')
->reactive()
->createOptionForm([
Forms\Components\TextInput::make('contact_name')->label('Contact persoon')->required(),
Forms\Components\Hidden::make('organisation_id')->default(Filament::getTenant()->id),
Forms\Components\TextInput::make('email')->label('Email')->email(),
Forms\Components\TextInput::make('company_name')->label('Bedrijfsnaam'),
Forms\Components\TextInput::make('phone')->label('Telefoon'),
Forms\Components\TextInput::make('vat_number')->label('Belastingnummer'),
Forms\Components\TextInput::make('kvk_number')->label('KVK nummer'),
]),
Select::make('relation_id')->label('Kies een relatie')
->searchable()
->preload()
->required()
->relationship('relation', 'contact_name')
->reactive()
->createOptionForm([
Forms\Components\TextInput::make('contact_name')->label('Contact persoon')->required(),
Forms\Components\Hidden::make('organisation_id')->default(Filament::getTenant()->id),
Forms\Components\TextInput::make('email')->label('Email')->email(),
Forms\Components\TextInput::make('company_name')->label('Bedrijfsnaam'),
Forms\Components\TextInput::make('phone')->label('Telefoon'),
Forms\Components\TextInput::make('vat_number')->label('Belastingnummer'),
Forms\Components\TextInput::make('kvk_number')->label('KVK nummer'),
]),
1 Reply
Basically this:
Change
This or you could also implement a global scope for your Relation model: https://laravel.com/docs/10.x/eloquent#global-scopes
modifyQueryUsing
according to your needs
Select::make('relation_id')->label('Kies een relatie')
->searchable()
->preload()
->required()
->relationship(
name: 'relation',
titleAttribute: 'contact_name',
modifyQueryUsing: fn (Builder $query) => $query->where('tenant_id', auth()->user()->tenant->id),
)
->reactive()
->createOptionForm([
Forms\Components\TextInput::make('contact_name')->label('Contact persoon')->required(),
Forms\Components\Hidden::make('organisation_id')->default(Filament::getTenant()->id),
Forms\Components\TextInput::make('email')->label('Email')->email(),
Forms\Components\TextInput::make('company_name')->label('Bedrijfsnaam'),
Forms\Components\TextInput::make('phone')->label('Telefoon'),
Forms\Components\TextInput::make('vat_number')->label('Belastingnummer'),
Forms\Components\TextInput::make('kvk_number')->label('KVK nummer'),
]),
Select::make('relation_id')->label('Kies een relatie')
->searchable()
->preload()
->required()
->relationship(
name: 'relation',
titleAttribute: 'contact_name',
modifyQueryUsing: fn (Builder $query) => $query->where('tenant_id', auth()->user()->tenant->id),
)
->reactive()
->createOptionForm([
Forms\Components\TextInput::make('contact_name')->label('Contact persoon')->required(),
Forms\Components\Hidden::make('organisation_id')->default(Filament::getTenant()->id),
Forms\Components\TextInput::make('email')->label('Email')->email(),
Forms\Components\TextInput::make('company_name')->label('Bedrijfsnaam'),
Forms\Components\TextInput::make('phone')->label('Telefoon'),
Forms\Components\TextInput::make('vat_number')->label('Belastingnummer'),
Forms\Components\TextInput::make('kvk_number')->label('KVK nummer'),
]),