F
Filamentβ€’2mo ago
Alexandre

$get() in Wizard Form is null

Hi all ! I have the same problem as this topic : https://discord.com/channels/883083792112300104/1159576155746213918/1159576155746213918 In my case, I've a Select form in the first step where the user choose a building (or can create new one) :
Wizard::make([
Step::make('step_1')
->label(...)
->schema([
Select::make('building')
->label(...)
->options(..)
->native(false)
->searchable()
->allowHtml()
->required()
->createOptionForm(...)
->createOptionModalHeading(...)
->createOptionUsing(...)
->live()
])
Wizard::make([
Step::make('step_1')
->label(...)
->schema([
Select::make('building')
->label(...)
->options(..)
->native(false)
->searchable()
->allowHtml()
->required()
->createOptionForm(...)
->createOptionModalHeading(...)
->createOptionUsing(...)
->live()
])
On step 2, I have another select in a repeater where I want to use the ID of the building field (step 1) to make a query search. So, I use Get $get for that :
Step::make('step_2')
->label(...)
->schema([
Repeater::make('products')
->label(...)
->schema([
Section::make('key')
->schema([
Select::make('card_number')
->getSearchResultsUsing(function (
string $search,
Get $get
): array {
dd($get('building')); //return null
})
->columnSpanFull(),
Step::make('step_2')
->label(...)
->schema([
Repeater::make('products')
->label(...)
->schema([
Section::make('key')
->schema([
Select::make('card_number')
->getSearchResultsUsing(function (
string $search,
Get $get
): array {
dd($get('building')); //return null
})
->columnSpanFull(),
The $get return null. If I do a $get of another field on the current step, it's work. Is there anything special I need to do to retrieve the field value in a repeater and form wizard? Thanks in advance πŸ™‚
Solution:
because I made a typo! data_get($livewire->data, 'building')...
Jump to solution
10 Replies
Alexandre
Alexandreβ€’2mo ago
Ok, I found that : https://filamentphp.com/docs/3.x/forms/fields/repeater#using-get-to-access-parent-field-values So, I try to use this : dd($get('../building')); (with one "../", two and three) but same result. Is there a way to DD the form structure?
Alexandre
Alexandreβ€’2mo ago
I found another way, but I don't know if it's a good one πŸ™ƒ
So... I'd like your opinion. I use the Livewire component to retrieve the field value and perform the search. The result is as follows:
->getSearchResultsUsing(function (
string $search,
Livewire $livewire
): array {
$building_id = $livewire->data['building'];

return Media::where('model_type',
'App\Models\building')->where('model_id',
$building_id)
->where('name',
'like', "%{$search}%")
->limit(50)
->pluck('name', 'id')
->toArray();
})
->getSearchResultsUsing(function (
string $search,
Livewire $livewire
): array {
$building_id = $livewire->data['building'];

return Media::where('model_type',
'App\Models\building')->where('model_id',
$building_id)
->where('name',
'like', "%{$search}%")
->limit(50)
->pluck('name', 'id')
->toArray();
})
And… It's work. Seem ok?
toeknee
toekneeβ€’2mo ago
You'd have to traverse back, you can see how far you need to traverse by running dd($get). Something like you said with ../../ likely needed more ../../../../building You can dom that but I would have done: data_get('building', $livewire->data, '')
Alexandre
Alexandreβ€’2mo ago
Thanks for your feedback. The docs specify that $get is scoped in the repeater when used inside. When I do a dd($get), I only get the select input in the second step, but not all fields. $livewire->data return an array with all fields and their values. If I do data_get('building', $livewire->data, ''); I've got an empty string in return. Same with data_get('building', $livewire->data['building'], ''); and data_get('../building', $livewire->data['building'], ''); I guess using $building_id = $livewire->data['building']; is valid?
toeknee
toekneeβ€’2mo ago
Yours is valid if building exists always!! You can also do: data_get('building', $livewire->data['building']);
Alexandre
Alexandreβ€’2mo ago
That's all right then, because you can't go on to step 2 if the "building" field is empty πŸ‘ However, the data_get returns null and I'm having a bit of trouble understanding why... Knowing that $livewire->data['building'] does return a result. I guess it's having trouble finding the 'building' field but I've tried with '../' and '../../' and it doesn't make any difference. However, as expressed in my first message, I just have 2 steps with a repeater in the second. I'm tempted to say that it's not too bad as long as $livewire->data is ok, but uh πŸ€·β€β™‚οΈ
toeknee
toekneeβ€’2mo ago
data_get returns null when there is no default valu. What does building return? It would be an empty string surely which was what we were previously providing? Yeah you are right though πŸ™‚
Alexandre
Alexandreβ€’2mo ago
$livewire->data['building'] return the ID of the option selected in the select building field (on step 1). For example, 168.
Solution
toeknee
toekneeβ€’2mo ago
because I made a typo! data_get($livewire->data, 'building')
Alexandre
Alexandreβ€’2mo ago
Aaaaahh okay, it happens even to the best. All is good now. Thanks, I will use it, didn't know that helper! πŸ˜‡