I'm trying to create a custom component for my Laravel filament project. Inside my ProductResource, I generate components code inside the foreach loop.
public static function form(Form $form): Form
{
foreach ($filters as $filter) {
$filterInputs[] = new ProductFilter('filter'.$filter->id, $filter_values[$filter->id] ?? null);
}
return $form
->schema([
Forms\Components\Group::make()
->schema([
Forms\Components\Section::make('Product Information')
])
->columnSpan(['lg' => 2]),
Forms\Components\Group::make()
->schema([
Forms\Components\Section::make('Filters')
->schema(array_merge([
View::make('custom.alert')
], $filterInputs)),
])
->columnSpan(['lg' => 1]),
])->columns(3);
}
My ProductFilter component
class ProductFilter extends Component
{
protected $defaultValue;
protected $name;
public function __construct($name, $defaultValue = null)
{
$this->name = $name;
$this->defaultValue = $defaultValue;
}
public function setUp() : void
{
parent::setUp();
if ($this->getState() === null) {
$this->state($this->defaultValue);
}
}
public function render(): View
{
return view('custom.product-filter', [
'id' => $this->getId(),
'name' => $this->name,
'model' => $this->getModel(),
'defaultValue' => $this->defaultValue,
]);
}
}
And it generates input correctly. It has a name, default value, etc. As an example:
<input type="text" id="" name="filter2" value="22" wire:model="filter2">
But the problem is, when I post product update form, I can't get filter2 value.
On EditProduct.php, I have save function to customize updating process.
public function save(bool $shouldRedirect = true): void
{
dd($this->data);
}
$this->data is not containing filter input values.
I tried to generate input by component and field. Also I tried without creating any component, just basic TextInput like;
foreach ($filters as $filter) {
$filterInputs[] = Forms\Components\TextInput::make('filter'.$filter->id)
->numeric()
->label($filter->title)
->default($filter->value);
}
In this scenario, my text inputs did not have the default data I specified.