How to get record of select relationship automatically
Select::make('type_infrastructure_id')
->relationship(name: 'typeInfrastructure', titleAttribute: 'nom')
->createOptionForm([
Forms\Components\TextInput::make('nom')
->required(),
Toggle::make('surface')
->onColor('success')
->offColor('danger'),
Toggle::make('circular')
->onColor('success')
->offColor('danger'),
Toggle::make('volume')
->onColor('success')
->offColor('danger'),
]),
TextInput::make('longueur')
->hidden(fn(\Filament\Forms\Get $get) => $get('type_infrastructure_id') ? $type_infrastructure_id->name === '///here' : true),
Select::make('type_infrastructure_id')
->relationship(name: 'typeInfrastructure', titleAttribute: 'nom')
->createOptionForm([
Forms\Components\TextInput::make('nom')
->required(),
Toggle::make('surface')
->onColor('success')
->offColor('danger'),
Toggle::make('circular')
->onColor('success')
->offColor('danger'),
Toggle::make('volume')
->onColor('success')
->offColor('danger'),
]),
TextInput::make('longueur')
->hidden(fn(\Filament\Forms\Get $get) => $get('type_infrastructure_id') ? $type_infrastructure_id->name === '///here' : true),
4 Replies
what is the issue? What are you trying to do?
Get type_infrastructure name from his id
If you want to get the name of the id, you should have a relationship in your model.
Did you add a belongsTo in your model?
yes
I have this but much redundancy
TextInput::make('length')
->hidden(fn(\Filament\Forms\Get $get) => $get('type_infrastructure_id') ? TypeInfrastructure::find($get('type_infrastructure_id'))->circular: true),
TextInput::make('width')
->hidden(fn(\Filament\Forms\Get $get) => $get('type_infrastructure_id') ? TypeInfrastructure::find($get('type_infrastructure_id'))->circular: true),
TextInput::make('diametre')
->hidden(fn(\Filament\Forms\Get $get) => $get('type_infrastructure_id') ? TypeInfrastructure::find($get('type_infrastructure_id'))->surface : true),
TextInput::make('depth')
->hidden(fn(\Filament\Forms\Get $get) => !$get('type_infrastructure_id') || !TypeInfrastructure::find($get('type_infrastructure_id'))->volume),
TextInput::make('level')
->hidden(fn(\Filament\Forms\Get $get) => !$get('type_infrastructure_id') || !TypeInfrastructure::find($get('type_infrastructure_id'))->volume)
->suffix('%')
->default(100),
TextInput::make('length')
->hidden(fn(\Filament\Forms\Get $get) => $get('type_infrastructure_id') ? TypeInfrastructure::find($get('type_infrastructure_id'))->circular: true),
TextInput::make('width')
->hidden(fn(\Filament\Forms\Get $get) => $get('type_infrastructure_id') ? TypeInfrastructure::find($get('type_infrastructure_id'))->circular: true),
TextInput::make('diametre')
->hidden(fn(\Filament\Forms\Get $get) => $get('type_infrastructure_id') ? TypeInfrastructure::find($get('type_infrastructure_id'))->surface : true),
TextInput::make('depth')
->hidden(fn(\Filament\Forms\Get $get) => !$get('type_infrastructure_id') || !TypeInfrastructure::find($get('type_infrastructure_id'))->volume),
TextInput::make('level')
->hidden(fn(\Filament\Forms\Get $get) => !$get('type_infrastructure_id') || !TypeInfrastructure::find($get('type_infrastructure_id'))->volume)
->suffix('%')
->default(100),