Set state for enum

How can I get the selected value for a select column when the date is filled from an enum?
Forms\Components\Select::make('type')->options(VendingMachineType::toAssociativeArray())
Forms\Components\Select::make('type')->options(VendingMachineType::toAssociativeArray())
the enum data returns;
[1 => 'item 1', 2 => 'item 2']
[1 => 'item 1', 2 => 'item 2']
18 Replies
Dennis Koch
Dennis Koch2y ago
There is no difference then setting other state, right? In this case it’s 1 or 2
Robin
RobinOP2y ago
I didn't run the migrations 🤣 well, anyow, another question on this matterç I have something like this;
Forms\Components\Select::make('sellingPoint.zone')
->reactive()
->options(Auth::user()?->tenant?->zones?->pluck('name', 'id'))
->afterStateUpdated(function (?VendingMachine $record, $state) {
$record?->sellingPoint?->update([
'zone_id' => $state
]);
}),
Forms\Components\Select::make('sellingPoint.zone')
->reactive()
->options(Auth::user()?->tenant?->zones?->pluck('name', 'id'))
->afterStateUpdated(function (?VendingMachine $record, $state) {
$record?->sellingPoint?->update([
'zone_id' => $state
]);
}),
But the state can't be set? I can't access $record in state() ? Or how am I supposed to do this? 😅
Dennis Koch
Dennis Koch2y ago
Use ->relationship() instead of afterStateUpdated
Robin
RobinOP2y ago
You can't do a $model->relation->relation? Call to undefined method App\Models\VendingMachine::sellingPoint.zone() Doing; Forms\Components\Select::make('sellingPoint.zone') ->relationship('sellingPoint.zone', 'name'), it should be this; ->state($this->getRecord()->sellingPoint->zone->id) but then you get Typed property Filament\Forms\Components\Component::$container must not be accessed before initialization
Dennis Koch
Dennis Koch2y ago
I thought nested relation were supported. You could wrap this in a Layout field with the sellingPoint relation and then just the select inside
Robin
RobinOP2y ago
huh I don't really understand what you mean here @Dennis Koch
Dennis Koch
Dennis Koch2y ago
Filament
Layout - Form Builder - Filament
The elegant TALL stack form builder for Laravel artisans.
Robin
RobinOP2y ago
But then I get something like this;
Robin
RobinOP2y ago
okay, not when I do it as a grid BUT
Forms\Components\Select::make('sellingPoint.zone')
->reactive()
->disabled() // TODO: fix
->options(Auth::user()?->tenant?->zones?->pluck('name', 'id'))
->afterStateUpdated(function (?VendingMachine $record, $state) {
$record?->sellingPoint?->update([
'zone_id' => $state
]);
}),
Forms\Components\Select::make('sellingPoint.zone')
->reactive()
->disabled() // TODO: fix
->options(Auth::user()?->tenant?->zones?->pluck('name', 'id'))
->afterStateUpdated(function (?VendingMachine $record, $state) {
$record?->sellingPoint?->update([
'zone_id' => $state
]);
}),
How the heck would this work
Dennis Koch
Dennis Koch2y ago
Does this work?
Fieldset::make('Selling Point')
->relationship('sellingPoint')
->schema([
Forms\Components\Select::make('zone')
->reactive()
->options(Auth::user()?->tenant?->zones?->pluck('name', 'id'))
]),
Fieldset::make('Selling Point')
->relationship('sellingPoint')
->schema([
Forms\Components\Select::make('zone')
->reactive()
->options(Auth::user()?->tenant?->zones?->pluck('name', 'id'))
]),
Robin
RobinOP2y ago
Yes and no When I save it, Add fillable property [zone] to allow mass assignment on [App\Models\SellingPoint]. but that's on a relation 🤔
Dan Harrin
Dan Harrin2y ago
sounds like a sane error to me?
Robin
RobinOP2y ago
Yes and no... it should use the zone_id, not the while zone object because when I add it to the mass assignment (which we should not) you see the query and it's something like;
Robin
RobinOP2y ago
Robin
RobinOP2y ago
oh god I'm an idiot
Forms\Components\Grid::make('Selling Point')
->relationship('sellingPoint')
->schema([
Forms\Components\Select::make('zone_id')
->label('Zone')
->searchable()
->options(auth()->user()?->tenant?->zones?->pluck('name', 'id'))
]),
Forms\Components\Grid::make('Selling Point')
->relationship('sellingPoint')
->schema([
Forms\Components\Select::make('zone_id')
->label('Zone')
->searchable()
->options(auth()->user()?->tenant?->zones?->pluck('name', 'id'))
]),
This worked 🙈 Thanks a lot for pointing the fact that the docs should be read in full 😅 LOVE FILAMENT (btw, we started using filament in all of our projects (+- 150)
Dennis Koch
Dennis Koch2y ago
150 projects?! 😅
Dan Harrin
Dan Harrin2y ago
looking forward to those sponsorships coming in from you/your happy clients! 😉
Robin
RobinOP2y ago
converting all our projects to filament if they have a shit or non admin area
Want results from more Discord servers?
Add your server