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

robjmills's avatar

Laravel and the EAV model

Hi,

Looking to implement the EAV model into a Laravel app to all users to extend the scope of objects with their own custom fields. Don't want to re-invent the wheel here so wondered how this fits in with Eloquent. I've seen that Cartalyst have an Attributes package ( https://cartalyst.com/manual/attributes ) and i'm a subscriber so that might be a good fit, anything else I should be taking a look at?

Thanks!

0 likes
14 replies
Felix's avatar

I doing the same, but I think if you want that flexibility you have to create these 10-15 tables with all the datatypes. But I think if you just want to store some metadata for users you don't need the whole EAV model. Depends what you want to do with data afterwards.

nate.a.johnson's avatar

I'm not saying don't do it, but EAV is typically considered an anti-pattern. It's very tempting but it can come with consequences later in your development/production cycle when you need what would be pretty simple queries in a non-EAV setup. There are lots of good articles describing this out there. Google it if you'd like to read up.

One thing to consider is a nosql database. You will still have some of the problems with EAV, but they were invented to scale well to no defined schema types of applications.

ATOM-Group's avatar

@nate.a.johnson

What are the alternatives to EAV? Imagine running a gaming community whereby you support League of Legends, DOTA, DOTA2, Heroes of Newerth, and the many MOBA clones out there.

The "characters" (heroes/champions/legends/whatevers) for each game have different attributes that describe them. LoL champions have different attributes from say, HoN heroes. How else would you be able to create a multi-tenancy CMS system for entering information about those characters without using EAV?

nate.a.johnson's avatar

Like I mentioned, I'm not saying EAV is always a bad decision. In the situation you described, I don't think analytical queries (ex: select all characters that have built up 200 HP, 100 MP, have a special weapon, and are in a party of 4 or more) are that important - you just want to retrieve a character, enemy or item and make sure it has all the attributes and relations that it needs even if they are all completely different entities. EAV becomes hard when you need lots of flexibility to query the data as described in my hypothetical query.

That said, I think you are describing an even better case for a nosql database and a clustered caching solution. If you're not familiar with nosql, just think of it as a way to persist objects in JSON form. If an object then has a new field added it it, no problem, it just adds to the JSON on the next save. Same goes for persisting relationships. In nosql there are no schemas so you can persist anything, anytime. It's pretty cool. And they scale very very well, and allow clustered caching with systems like memcached.

Again, I'm not saying you should pick nosql, EAV, or non-EAV. You need to weigh the factors for your particular app, research what's out there, and make the best decision that you can for your problem domain.

Hope that helps.

1 like
IsraelOrtuno's avatar

@Erik I am still working on it, will release a first version in a few days. Hopefully community will contribute and make it more robust.

It would also be great to rewrite this package to add NoSQL support.

thepsion5's avatar

@tag What would be hard about implementing this in the form of a single table for all of the attributes shared between hero types and then using a secondary table for game-specific attributes?

Sure, it'll end up being a lot of tables, but that's just choosing how to organize sets of complex data. I'd rather have it organized into two dozen game-specific tables than a single massive table of key-value pairs.

IsraelOrtuno's avatar

I'm with @thepsion5, if you know in advance your requirements, let's go for the traditional way. I'm thinking of EAV to give the end user a little bit or customization over a few entities.

ronnyandre's avatar

Just found this thread, and interesting to see that this package is still alive and being actively developed. Is there some kind of simple documentation to get me started?

benr1804's avatar

Hi @IsraelOrtuno Are you still developing the EAVquent package? I am actually using it in production on an app I have developed. Just wondering if you are still in the process of rewriting it?

Please or to participate in this conversation.