Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

Custom Fields for E-Shops: EAV Model

Premium
5 min read

There are situations when we're not sure what the columns of the DB table would be, they need to be flexible. For example, e-shop product properties: size, color, fabric, there may be more in the future. What is the best DB structure? I will show you three options and their performance.


Option 1. Properties DB Table and EAV Model

We can define a separate DB table properties and then a Pivot table product_property with many-to-many relations, and an extra field for the value.

This DB structure implements the so-called EAV (Entity-Attribute-Value) Model.

database/migrations/xxx_create_product_property_table.php:

Schema::create('product_property', function (Blueprint $table) {
$table->foreignId('product_id')->constrained();
$table->foreignId('property_id')->constrained();
$table->string('value');
});

app/Models/Product.php:

class Product extends Model
{
public function properties()
{
return $this->belongsToMany(Property::class)
->withPivot('value');
}
}

Then, if we want to filter the products by specific few property values (like, "Size L and color red"), we can do this.

Controller:

Product::query()
->whereHas('properties', function($query) {
$query->where('properties.name', 'size')
->where('product_property.value', 'L');
})
->whereHas('properties', function($query) {
$query->where('properties.name', 'color')
->where('product_property.value', 'red');
})
->get();

Notice: you may skip the ::query(), I add it personally to have the well-formatted following lines of code, for better readability.

This structure gives you the flexibility to add...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (29 h 14 min)

You also get:

54 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

S
Sisifo ✓ Link copied!

Hi,

Thanks for updating the course.

I understand the recomendation of "avoid EAV model unless your REALLY need". However, if it will be necessary to take it a bit further with different types of "values", not only strings... I can imagine: date, intigers, "selections" (different accotated options), booleans... what would be the structure? (I understand it is not a quick answer...) Could you provide with a example in a third package or a external resource?

Really thanks in advance