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),
Was this page helpful?