F
Filament5mo ago
Stefan

attach properties to products

I want to assign properties to the products: Product Model:
public function properties()
{
return $this->hasMany(ProductProperty::class);
}
public function properties()
{
return $this->hasMany(ProductProperty::class);
}
Product Table products fields:
id,name,etc.
id,name,etc.
I have registered PropertiesRelationManager::class on the ProductRequest Property Model:
public function values()
{
return $this->hasMany(PropertyValue::class);
}
public function values()
{
return $this->hasMany(PropertyValue::class);
}
Property Table properties fields:
id,name,etc.
id,name,etc.
PropertyValue Model:
public function product()
{
return $this->belongsTo(Product::class);
}
public function product()
{
return $this->belongsTo(Product::class);
}
Table property_values with the followings fields:
id,property_id,value,etc.
id,property_id,value,etc.
ProductProperty Model:
public function product()
{
return $this->belongsTo(Product::class);
}
public function product()
{
return $this->belongsTo(Product::class);
}
ProductProperty Table product_properties fields:
id,product_id,property_id,property_value_id,etc.
id,product_id,property_id,property_value_id,etc.
PropertiesRelationManager:
public function form(Form $form)
{
return $form
->schema([
Select::make('property_id')
->options(Property::all()->pluck('name', 'id'))
->live(),
Select::make('property_value_id')
->options(fn (Forms\Get $get): Collection => PropertyValue::query()
->where('property_id', $get('property_id'))
->pluck('value', 'id'))
->multiple()
->live(),
]);
}
public function form(Form $form)
{
return $form
->schema([
Select::make('property_id')
->options(Property::all()->pluck('name', 'id'))
->live(),
Select::make('property_value_id')
->options(fn (Forms\Get $get): Collection => PropertyValue::query()
->where('property_id', $get('property_id'))
->pluck('value', 'id'))
->multiple()
->live(),
]);
}
When I save, I get: Array to string conversion I guess that this happens because I declared multiple() on Select::make('property_value_id') and what I select is passed as array and my field property_value_id accepts an integer. For each selected value in this Select::make('property_value_id') I want to add a record in the product_properties table not a single record with all the values as if the property_value_id would be of type json. Thanks for your time!
0 Replies
No replies yetBe the first to reply to this messageJoin