Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mertasan's avatar

Model and controller structure for product variations

I have an existing scheme for products variations.

I want to create a combination of each production time, quantities and variation options.

I will set also price separately for each combination.

However, I could not establish the relationship between model, controller and blade.

I will create a selection form by accessing the quantities, production times, variation and variation options from the product.

Can you show me an example structure on how to establish the Model, Controller and Blade(foreach) relationship?

In addition, how should the database table i will to store prices/combinations?

My DB Diagram

According to the planned job, the product detail page will be as follows:

Form Wizard:

<label> Day (Production Time) </label>
<select>
<option selected> 1 Day </option>
<option> 2 Days </option>
</select>

<label> Quantity </label>
<select>
<option selected> 100 </option>
<option> 200 </option>
</select>

<label> Color </label>
<select>
<option selected> Red </option>
<option> Yellow </option>
</select>

<label> Size </label>
<select>
<option selected> Small </option>
<option> Medium </option>
<option> Large </option>
</select>

Results (combination): 1 Day - 100 qt - Red - Small

Total price: [Combination price]

0 likes
9 replies
martinbean's avatar

@yediyuz When I model this in e-commerce applications, there are two models: a Product (which holds common details such as name) and then multiple Skus. A Sku model holds a particular variation’s details, including price.

In your case, I’d also have another model: ProductAttribute. This model would hold the name of an attribute (such as “Color”), and then a pivot table between attributes and SKUs that also has a pivot column for the value. This way, for each SKU, you can see its colour, size, etc.

In terms of controllers, SKUs would then become a “nested” resource under products, making your URI structure look similar to this:

  • /products/{products}/skus
  • /products/{products}/skus/{sku}
1 like
aurawindsurfing's avatar

@martinbean do you mind me asking how do you handle then the front end? Obviously attributes can be grouped in colours sizes etc. How one creates selector that then will select attributes but then under the hood select correct sku for it?

martinbean's avatar

@aurawindsurfing Depends on the number of attributes, really. For products with just one attribute (say, colour) I usually just present a list with radio buttons to select the specific SKU. For products with more than one attribute, you can use something like Livewire or JavaScript and present select options for each attribute. As you choose an attribute value, it can filter the options in the other attribute select inputs.

aurawindsurfing's avatar

@martinbean Yeah ended up with both Livewire and Alpine, still feels really hacky with multiple group of attributes and constant switching between arrays and collections ;-)

I thought there was maybe a neat way of building a map of variants + map of available attributes + map of in stock variants and joining it somehow for the good dev experience but I guess its only my wishful thinking ;-)

mertasan's avatar

@martinbean Thank you so much for using your time to respond. I've been using PHP for a long time. However, I met Laravel 2 weeks ago.

I've gone through many stages but I don't know what to do at this stage. I have established various relationships between models (belongsToMany, hasMany etc.) But I could not come to an exact conclusion.

I can proceed the way you suggest. But if you have time, can you add a small sample model and controller structure?

mertasan's avatar

is there anyone who can help with this?

Please or to participate in this conversation.