Pre-populating form and saving to pivot

4 tables and one pivot table: Products, Product_Options, Product_Option_Values, and Product_Variants. The pivot table essentially just joins the 3 latter tables with unique constraints. I've made a custom pivot model for this and the relationship for the variant is selectedOptions which you'll see below. The user creates a product, then creates its "Product Options" with available "Values". For example, Size: SM, MD, LG and Color: Red, Blue This works fine. The issue I'm encountering is when the user goes to create a new Product Variant (via a relation manage on the Product), I want to loop through all available Product Options and have them select a Value. I've tried a number of things but none of it seems to be working correctly. This is the gist of one route I went:
1 Reply
schwartzmj
schwartzmjOP10mo ago
Forms\Components\Repeater::make('selectedOptions')
->relationship('selectedOptions')
->schema(
...$this->getOwnerRecord()->productOptions()->get()->map(function ($productOption) {
return [
Forms\Components\Select::make('product_option_value_id')
->relationship('productOptionValue', 'value')
->options($productOption->productOptionValues->pluck('value', 'id')->toArray())
->label($productOption->name)
->required(),
Forms\Components\Select::make('product_option_id')
->relationship('productOption', 'name')
->options([$productOption->id => $productOption->name])
->default($productOption->id),
Forms\Components\TextInput::make('product_option_id')
->default($productOption->id)
];
})->toArray()
)
Forms\Components\Repeater::make('selectedOptions')
->relationship('selectedOptions')
->schema(
...$this->getOwnerRecord()->productOptions()->get()->map(function ($productOption) {
return [
Forms\Components\Select::make('product_option_value_id')
->relationship('productOptionValue', 'value')
->options($productOption->productOptionValues->pluck('value', 'id')->toArray())
->label($productOption->name)
->required(),
Forms\Components\Select::make('product_option_id')
->relationship('productOption', 'name')
->options([$productOption->id => $productOption->name])
->default($productOption->id),
Forms\Components\TextInput::make('product_option_id')
->default($productOption->id)
];
})->toArray()
)
My example is a bit messy but I'm throwing things at the wall here to see what sticks. Hoping somebody can help point me in the right direction.
Want results from more Discord servers?
Add your server