PHPackages                             mralston/laravel-eav - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. mralston/laravel-eav

ActiveLibrary[Database &amp; ORM](/categories/database)

mralston/laravel-eav
====================

Implements the Entity-Attribute-Value pattern to supplement Eloquent with adhoc attributes.

v1.0.2(2y ago)39MITPHPPHP ^8.0

Since Nov 5Pushed 1y ago2 watchersCompare

[ Source](https://github.com/mralston/laravel-eav)[ Packagist](https://packagist.org/packages/mralston/laravel-eav)[ Docs](https://github.com/mralston/laravel-eav)[ RSS](/packages/mralston-laravel-eav/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (3)Dependencies (2)Versions (4)Used By (0)

Laravel EAV
===========

[](#laravel-eav)

Introduction
------------

[](#introduction)

Entity Attribute Value package for Laravel.

EAV Definition From [Wikipedia](https://en.wikipedia.org/wiki/Entity%E2%80%93attribute%E2%80%93value_model):

Entity–attribute–value model (EAV) is a data model to encode, in a space-efficient manner, entities where the number of attributes (properties, parameters) that can be used to describe them is potentially vast, but the number that will actually apply to a given entity is relatively modest.

**TL;DR** this package allows you to work with attributes on your models which aren't defined in the underlying database schema.

But why though?
---------------

[](#but-why-though)

> Your scientists were so preoccupied with whether or not they could that they didn't stop to think if they should.
>
> \-- Dr. Ian Malcolm (Jeff Goldblum), Jurassic Park

- Yes, there are another packages which already provide similar functionality.
- Yes, EAV is sometimes considered an anti-pattern due to performance issues.
- Yes, this might be considered reinventing the NoSQL wheel.
- Yes, I did it anyway. 🤷🏻‍♂️

Truth be told, this started as a proof-of-concept based on an idea I had, aimed at improving a messy pre-existing implementation in several projects which I am involved in maintaining.

The EAV implementations which I've seen thus far have a separate database record for each EAV attribute. I believe that the bulk of the performance problems comes from the large amounts of additional database traffic as a result.

My concept is to place all of the EAV data into a JSON field in a single database record, which can be fetched just once per model instance.

Inspiration for using a [JSON field](https://www.youtube.com/watch?v=QZBxgX2OWbI) came from [Aaron Francis](https://aaronfrancis.com/) @ PlanetScale, so kudos to him. Also thanks to Aaron for the get-out-of-jail-free card when it comes to the 'you should just use a NoSQL database' argument.

Installation
------------

[](#installation)

Install the package using Composer:

```
composer require mralston/laravel-eav
```

Config
------

[](#config)

This package has no config, it doesn't need it. Yay!

Migrations
----------

[](#migrations)

After installing, you need to migrate the package's single database table.

```
php artisan migrate
```

Usage
-----

[](#usage)

This package is designed to be completely transparent. You can read and write to model attributes just as you normally would.

Any model which you would like to use EAV will need to use the `HasEntityAttributeValues` trait:

```
