Dependant select with BelongsToMany relation

Hi, im my app i have a dependant select with belongsTo that works fine i'm trying to recreate the same concept with another couple of select but this time they have a belongsToMany relation more in dept, the first select shows a list of customer and the second select a list of companies that the selected customer are associated this is the code for the first example (belongsTo)
Forms\Components\Select::make('brand_id')
->relationship(name: 'brand', titleAttribute: 'name')
->required()
->preload()
->live()
->afterStateUpdated(fn (Set $set) => $set('item_id', null))
->native(false)
->searchable()
->getSearchResultsUsing(fn (string $search) => Brand::where('name', 'like', "%{$search}%")->limit(50)->pluck('name', 'id'))
->getOptionLabelUsing(fn ($value): ?string => Brand::find($value)?->name),

Forms\Components\Select::make('item_id')
->relationship(
name: 'item',
modifyQueryUsing: fn (Get $get, Builder $query) => $query->where('brand_id', $get('brand_id'))->orderBy('name'),
)
->getOptionLabelFromRecordUsing(fn (Model $record) => "{$record->brand->name} {$record->name} {$record->model}")
->searchable(['name'])
->label('Item')
->required()
->preload()
->native(false)
->searchable()
->getSearchResultsUsing(fn (string $search) => Brand::where('name', 'like', "%{$search}%")->limit(50)->pluck('name', 'id'))
->getOptionLabelUsing(fn ($value): ?string => Brand::find($value)?->name),
Forms\Components\Select::make('brand_id')
->relationship(name: 'brand', titleAttribute: 'name')
->required()
->preload()
->live()
->afterStateUpdated(fn (Set $set) => $set('item_id', null))
->native(false)
->searchable()
->getSearchResultsUsing(fn (string $search) => Brand::where('name', 'like', "%{$search}%")->limit(50)->pluck('name', 'id'))
->getOptionLabelUsing(fn ($value): ?string => Brand::find($value)?->name),

Forms\Components\Select::make('item_id')
->relationship(
name: 'item',
modifyQueryUsing: fn (Get $get, Builder $query) => $query->where('brand_id', $get('brand_id'))->orderBy('name'),
)
->getOptionLabelFromRecordUsing(fn (Model $record) => "{$record->brand->name} {$record->name} {$record->model}")
->searchable(['name'])
->label('Item')
->required()
->preload()
->native(false)
->searchable()
->getSearchResultsUsing(fn (string $search) => Brand::where('name', 'like', "%{$search}%")->limit(50)->pluck('name', 'id'))
->getOptionLabelUsing(fn ($value): ?string => Brand::find($value)?->name),
1 Reply
Soundmit
Soundmit7mo ago
this is the basic version of the second example, with this code i can see all the companies regardless of the selected customer
Forms\Components\Select::make('customer_id')
->required()
->native(false)
->live()
->searchable()
->preload()
->afterStateUpdated(fn (Set $set) => $set('company_id', null))
->relationship(name: 'customer', titleAttribute: 'customer_name'),
Forms\Components\Select::make('company_id')
->native(false)
->searchable()
->preload()
->relationship(
name: 'company',
titleAttribute: 'name',
),
Forms\Components\Select::make('customer_id')
->required()
->native(false)
->live()
->searchable()
->preload()
->afterStateUpdated(fn (Set $set) => $set('company_id', null))
->relationship(name: 'customer', titleAttribute: 'customer_name'),
Forms\Components\Select::make('company_id')
->native(false)
->searchable()
->preload()
->relationship(
name: 'company',
titleAttribute: 'name',
),
this part doesn't works because is not for the belongsToMany relation (i think)
modifyQueryUsing: fn (Get $get, Builder $query) => $query->where('brand_id', $get('brand_id'))->orderBy('name'),
modifyQueryUsing: fn (Get $get, Builder $query) => $query->where('brand_id', $get('brand_id'))->orderBy('name'),
SOLUTION
Forms\Components\Select::make('customer_id')
->required()
->native(false)
->live()
->searchable()
->preload()
->afterStateUpdated(fn (Set $set) => $set('company_id', null))
->relationship(name: 'customer', titleAttribute: 'customer_name'),

Forms\Components\Select::make('company_id')
->native(false)
->searchable()
->preload()
->relationship(
name: 'company',
titleAttribute: 'name',
modifyQueryUsing: function (Get $get, Builder $query) {
$query->whereHas('customers', function ($customerQuery) use ($get) {
$customerQuery->where('customer_id', $get('customer_id'));
})->orderBy('name');
},
),
Forms\Components\Select::make('customer_id')
->required()
->native(false)
->live()
->searchable()
->preload()
->afterStateUpdated(fn (Set $set) => $set('company_id', null))
->relationship(name: 'customer', titleAttribute: 'customer_name'),

Forms\Components\Select::make('company_id')
->native(false)
->searchable()
->preload()
->relationship(
name: 'company',
titleAttribute: 'name',
modifyQueryUsing: function (Get $get, Builder $query) {
$query->whereHas('customers', function ($customerQuery) use ($get) {
$customerQuery->where('customer_id', $get('customer_id'));
})->orderBy('name');
},
),