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
LeandroFerreira
LeandroFerreira2mo ago
I think you could use PackageCard state to do this
reinterpret_cast<int*>(ptr) = -1
can you elaborate
LeandroFerreira
LeandroFerreira2mo 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
reinterpret_cast<int*>(ptr) = -1
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?
reinterpret_cast<int*>(ptr) = -1
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 ?
reinterpret_cast<int*>(ptr) = -1
the form wizard code
No description
reinterpret_cast<int*>(ptr) = -1
PackageCard:
No description
reinterpret_cast<int*>(ptr) = -1
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 Koch2mo 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
reinterpret_cast<int*>(ptr) = -1
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 Koch2mo ago
Sorry, I don't have experience with wizards
Want results from more Discord servers?
Add your server