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.
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');
}),
And below is my Field class
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());

}

}
I am using my field view like
<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:
Here is how issue is resolved. In form I used ``` Select::make('brand_id') ->relationship(name: 'brand', titleAttribute: 'name') ->searchable() ->preload() ->optionsLimit(5)...
Jump to solution
2 Replies
Pritbor
PritborOP3w ago
BTW my livewire is perfect, If $getMeta('modelId') is returning valid value then livewire works as expected else.
Solution
Pritbor
Pritbor3w ago
Here is how issue is resolved. In form I used
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');
}),
And in Custom Field class I used
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);
}

}
Finally i rendered my livewire in field view class like
<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()" />
Want results from more Discord servers?
Add your server