attach properties to products - many to many relation
I have ProductResource and I want to associate some Properties to each product. For this I made a PropertiesRelationalManager
The PropertyResource is bind of Property model and to the properties table. The properties table have the 2 columns, id and name
The property values are stored in the property_values table which have the following columns: id, property_id and value
I have a pivot table product_property with the following columns: id, product_id, property_value_id, property_id and quantity
I created a PropertiesRelationManager and registered on the ProductResource and it’s form method looks like this:
public function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Select::make('property_id')->label('Properties')
->relationship('properties', 'name')
->options(Property::all()->pluck('name', 'id'))
->live()
->required(),
Forms\Components\Select::make('property_value_id')->label('Values')
->relationship('properties.values', 'value')
->options(function (Forms\Get $get) {
$property = Property::find($get('property_id'));
return $property ? $property->values->pluck('value', 'id') : [];
})
->live()
->required(),
Forms\Components\TextInput::make('quantity'),
]);
}
Now when I want to save, I get Call to a member function associate() on null
Any help will be much appreciated!
1 Reply
The
associate()
method is used in BelongsTo
relationships, but everything seems to point to a BelongsToMany
relationship between Product and Property (given the use of a pivot table). Do you mind sharing the code of these two Models (Product & Property). I feel like there might be a problem with the relationship definition.