Custom Field passing dynamic data challenge
I am trying to pass the value to a custom field based on the state of other field using Get. But I am unable to retrieve data while i use the field. And below is my Field class I am using my field view like
Select::make('brand_id')
->relationship(name: 'brand', titleAttribute: 'name')
->searchable()
->preload()
->optionsLimit(5)
->required()
->live()
->afterStateUpdated(function (Set $set) {
$set('business_category_id', null);
}),
SelectItemModelBusinessCategory::make('business_category_id')
->label('Business Category')
->brandModel(function(Get $get): int {
return (int)$get('brand_id');
}),
Select::make('brand_id')
->relationship(name: 'brand', titleAttribute: 'name')
->searchable()
->preload()
->optionsLimit(5)
->required()
->live()
->afterStateUpdated(function (Set $set) {
$set('business_category_id', null);
}),
SelectItemModelBusinessCategory::make('business_category_id')
->label('Business Category')
->brandModel(function(Get $get): int {
return (int)$get('brand_id');
}),
class SelectItemModelBusinessCategory extends Field
{
protected string $view = 'forms.components.select-item-model-business-category';
protected int| Closure | null $modelId = null;
public function brandModel(int | Closure | null $modelId): static
{
$this->modelId = $modelId;
return $this;
}
public function getBrandModel(): ?int
{
return $this->evaluate($this->modelId);
}
/**
* Pass modelId to the view
*/
protected function setUp(): void
{
parent::setUp();
// Ensure modelId is passed to the view
$this->meta('modelId', $this->getBrandModel());
}
}
class SelectItemModelBusinessCategory extends Field
{
protected string $view = 'forms.components.select-item-model-business-category';
protected int| Closure | null $modelId = null;
public function brandModel(int | Closure | null $modelId): static
{
$this->modelId = $modelId;
return $this;
}
public function getBrandModel(): ?int
{
return $this->evaluate($this->modelId);
}
/**
* Pass modelId to the view
*/
protected function setUp(): void
{
parent::setUp();
// Ensure modelId is passed to the view
$this->meta('modelId', $this->getBrandModel());
}
}
<livewire:filament.categories.item-model-business-category-selection
wire:model.live="{{ $getStatePath() }}"
:modelId="$getMeta('modelId')"
:key="'item-model-business-category-' . $getMeta('modelId')" />
<livewire:filament.categories.item-model-business-category-selection
wire:model.live="{{ $getStatePath() }}"
:modelId="$getMeta('modelId')"
:key="'item-model-business-category-' . $getMeta('modelId')" />
Solution:Jump to solution
Here is how issue is resolved. In form I used ``` Select::make('brand_id')
->relationship(name: 'brand', titleAttribute: 'name')
->searchable()
->preload()
->optionsLimit(5)...
2 Replies
BTW my livewire is perfect, If $getMeta('modelId') is returning valid value then livewire works as expected else.
Solution
Here is how issue is resolved. In form I used And in Custom Field class I used Finally i rendered my livewire in field view class like
Select::make('brand_id')
->relationship(name: 'brand', titleAttribute: 'name')
->searchable()
->preload()
->optionsLimit(5)
->required()
->live()
->afterStateUpdated(function (Set $set, $state) {
$set('business_category_id', null);
}),
SelectItemModelBusinessCategory::make('business_category_id')
->label('Business Category')
->modelId(function ( Get $get): int {
return $get('brand_id');
}),
Select::make('brand_id')
->relationship(name: 'brand', titleAttribute: 'name')
->searchable()
->preload()
->optionsLimit(5)
->required()
->live()
->afterStateUpdated(function (Set $set, $state) {
$set('business_category_id', null);
}),
SelectItemModelBusinessCategory::make('business_category_id')
->label('Business Category')
->modelId(function ( Get $get): int {
return $get('brand_id');
}),
class SelectItemModelBusinessCategory extends Field
{
protected string $view = 'forms.components.select-item-model-business-category';
// protected ?int $modelId = null;
protected int | Closure | null $modelId = null;
/**
* Pass modelId to the view
*/
protected function setUp(): void
{
parent::setUp();
}
public function modelId(int | Closure | null $modelId): static
{
$this->modelId = $modelId;
return $this;
}
public function getModelId(): ?int
{
return $this->evaluate($this->modelId);
}
}
class SelectItemModelBusinessCategory extends Field
{
protected string $view = 'forms.components.select-item-model-business-category';
// protected ?int $modelId = null;
protected int | Closure | null $modelId = null;
/**
* Pass modelId to the view
*/
protected function setUp(): void
{
parent::setUp();
}
public function modelId(int | Closure | null $modelId): static
{
$this->modelId = $modelId;
return $this;
}
public function getModelId(): ?int
{
return $this->evaluate($this->modelId);
}
}
<livewire:filament.categories.item-model-business-category-selection
wire:model.live="{{ $getStatePath() }}"
:modelId="$getModelId()"
:key="'item-model-business-category-' . $getModelId()" />
<livewire:filament.categories.item-model-business-category-selection
wire:model.live="{{ $getStatePath() }}"
:modelId="$getModelId()"
:key="'item-model-business-category-' . $getModelId()" />