F
Filament8mo ago

custom field viewData closure returns empty always

hi , im trying to make a custom item list cards for packages the following implementation works fine
class PackageCard extends Field
{
protected string $view = 'forms.components.package-card';
}

//in resource
PackageCard::make("package_id")
->viewData([
'packages' => Package::query()->get()
])
//package-card.blade.php:
<x-dynamic-component
:component="$getFieldWrapperView()"
:field="$field"
wire:ignore
>
<div x-data="{ state: $wire.$entangle('{{ $getStatePath() }}') }">
<!-- Interact with the `state` property in Alpine.js -->
@foreach($packages as $package)
<p>{{$package->name}}</p>
@endforeach
</div>
</x-dynamic-component>
class PackageCard extends Field
{
protected string $view = 'forms.components.package-card';
}

//in resource
PackageCard::make("package_id")
->viewData([
'packages' => Package::query()->get()
])
//package-card.blade.php:
<x-dynamic-component
:component="$getFieldWrapperView()"
:field="$field"
wire:ignore
>
<div x-data="{ state: $wire.$entangle('{{ $getStatePath() }}') }">
<!-- Interact with the `state` property in Alpine.js -->
@foreach($packages as $package)
<p>{{$package->name}}</p>
@endforeach
</div>
</x-dynamic-component>
however when i do the following it doesn't return anything ,
'packages' => fn(Forms\Get $get): Collection => Package::query()->where('country_id', $get("country_id"))->get()

//country select in the resource :

Select::make('country_id')
->options(Country::all()->pluck('name', 'id'))
->label(__("Country"))
->suffixIcon('heroicon-m-map-pin')
->hint(__("Please select the desired country of incorporation."))
->required()
->live()
->preload()
->searchable()
'packages' => fn(Forms\Get $get): Collection => Package::query()->where('country_id', $get("country_id"))->get()

//country select in the resource :

Select::make('country_id')
->options(Country::all()->pluck('name', 'id'))
->label(__("Country"))
->suffixIcon('heroicon-m-map-pin')
->hint(__("Please select the desired country of incorporation."))
->required()
->live()
->preload()
->searchable()
what am i missing or doing wrong ? any help is appreciated
18 Replies
卵
OP8mo ago
bump
LeandroFerreira
LeandroFerreira8mo ago
I think you could use PackageCard state to do this
卵
OP8mo ago
can you elaborate
LeandroFerreira
LeandroFerreira8mo ago
use Filament\Forms\Components\Concerns\HasOptions;
class PackageCard extends Field
{
use HasOptions;
...
}
use Filament\Forms\Components\Concerns\HasOptions;
class PackageCard extends Field
{
use HasOptions;
...
}
PackageCard::make("package_id")
->options(fn (Get $get): Collection => Package::whereCountryId($get('country_id'))->get())
PackageCard::make("package_id")
->options(fn (Get $get): Collection => Package::whereCountryId($get('country_id'))->get())
@if($items = $getOptions())
@foreach($items as $item)
<div>{{ $item['name'] }}</div>
@endforeach
@endif
@if($items = $getOptions())
@foreach($items as $item)
<div>{{ $item['name'] }}</div>
@endforeach
@endif
卵
OP8mo ago
thanks for your reply saying i want to make checkboxs for the items to select one of them , how can i use the state here to tell my field i selected an item using alpinejs state?
卵
OP8mo ago
sorry for my late reply , we had a power outage thanks for your help as always i tried this and for some reason doing a where closure will cause it to not show any options , if we get all the packages it will work fine and display them it's kinda a weird behavior , have any idea why and what cause that ?
卵
OP8mo ago
the form wizard code
No description
卵
OP8mo ago
PackageCard:
No description
卵
OP8mo ago
blade :
卵
OP8mo ago
No description
卵
OP8mo ago
it seems whether i use viewData or options the same issue persists with the use of closure will only not show any data
Dennis Koch
Dennis Koch8mo ago
where closure will cause it to not show any options
Did you check what $get('country_id') returns? if the where condition returns zero results? I think the issues is: - You are rendering the options once and wire:ignore updates - Then the field is changed, but the options aren't updated in the blade file
卵
OP8mo ago
goddam that wire:ignore gave me couple hours of headache 🤦 much appreciated @Dennis Koch if it's possible to ask another question ! in my wizard i want to do a pop up in the first step when a user press next which will give me the ability to show/hide a step along the way , how can i do that , i searched but couldn't find anything
Dennis Koch
Dennis Koch8mo ago
Sorry, I don't have experience with wizards
卵
OP8mo ago
any idea who can help ?
Dennis Koch
Dennis Koch8mo ago
Best to start a new thread with a good headline. So this solved it?
卵
OP8mo ago
yes marked as solved posted another thread

Did you find this page helpful?