How do i get to the data while doing resource create?

I'm trying to change some select field options based another selection. During edit I can use $record, but that's always null during create.
Solution:
Probably using $get to get the value of the other field. Then you can do what you need with the value.
Jump to solution
11 Replies
skeemer
skeemerOP2y ago
Forms\Components\Select::make('logo_price_id')
->label('Logo Price Id')
->nullable()
->options(fn (Plan $record) => $record->partner?->getPriceOptions() ?? []),
Forms\Components\Select::make('logo_price_id')
->label('Logo Price Id')
->nullable()
->options(fn (Plan $record) => $record->partner?->getPriceOptions() ?? []),
awcodes
awcodes2y ago
This is obviously a relationship, so you can’t do that on create. You would have to return a default set of options from the Partner table for this to work on create.
skeemer
skeemerOP2y ago
I can still resolve from the selected id. So what's the injection for the data during create? There's no default since I'm pulling the options from Stripe based on the account selected.
Solution
awcodes
awcodes2y ago
Probably using $get to get the value of the other field. Then you can do what you need with the value.
skeemer
skeemerOP2y ago
Thank you, I forgot about that one since I primarily used Forms by itself until recently.
awcodes
awcodes2y ago
So,
options(function(Get $get, $operation, $record) {
if $operation === ‘create’ use $get
else use $record
}
options(function(Get $get, $operation, $record) {
if $operation === ‘create’ use $get
else use $record
}
skeemer
skeemerOP2y ago
probably easier to just use null coalescing
awcodes
awcodes2y ago
Whatever works for you. Cheers. Just saying the $operation is how you can determine what process you’re in. If you need it. The ternary could start to look gross if it has to make a call or query to get the data. 🙂
skeemer
skeemerOP2y ago
Forms\Components\Select::make('logo_price_id')
->label('Logo Price Id')
->nullable()
->options(fn (?Plan $record, Forms\Get $get) => ($record ?? Plan::find($get('partner_id')))?->partner?->getPriceOptions() ?? []),
Forms\Components\Select::make('logo_price_id')
->label('Logo Price Id')
->nullable()
->options(fn (?Plan $record, Forms\Get $get) => ($record ?? Plan::find($get('partner_id')))?->partner?->getPriceOptions() ?? []),
no ternary needed oops, that's not quite right
Forms\Components\Select::make('logo_price_id')
->label('Logo Price Id')
->nullable()
->options(fn (?Plan $record, Forms\Get $get) => ($record?->partner ?? Partner::find($get('partner_id')))?->getPriceOptions() ?? []),
Forms\Components\Select::make('logo_price_id')
->label('Logo Price Id')
->nullable()
->options(fn (?Plan $record, Forms\Get $get) => ($record?->partner ?? Partner::find($get('partner_id')))?->getPriceOptions() ?? []),

Did you find this page helpful?