PHPackages                             necenzurat/eloquent-meta - 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. necenzurat/eloquent-meta

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

necenzurat/eloquent-meta
========================

Fluent Meta Data for Eloquent Models, as if it is a property on your model.

1.0.5(5y ago)28.6kMITPHPPHP ^7.2.5|^8.0

Since Oct 2Pushed 5y ago1 watchersCompare

[ Source](https://github.com/necenzurat/eloquent-meta)[ Packagist](https://packagist.org/packages/necenzurat/eloquent-meta)[ RSS](/packages/necenzurat-eloquent-meta/feed)WikiDiscussions master Synced 5d ago

READMEChangelog (6)Dependencies (2)Versions (7)Used By (0)

Fluent Meta Data for Eloquent Models
====================================

[](#fluent-meta-data-for-eloquent-models)

[![Laravel](https://camo.githubusercontent.com/9392e682b195e01d5a658f91f6a63ae5213c64d557887643c897cedbfb52c0a1/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c61726176656c2d7e352e312d6f72616e67652e7376673f7374796c653d666c61742d737175617265)](http://laravel.com)[![Latest Version](https://camo.githubusercontent.com/a5219091e7e6db711598f1ff7672db90cfe136759c2873ed339149ed203eec70/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6e6563656e7a757261742f656c6f7175656e742d6d6574612e7376673f7374796c653d666c61742d626c7565)](https://packagist.org/packages/necenzurat/eloquent-meta)[![Downloads](https://camo.githubusercontent.com/dfaf5f115925866f11e4008481c31961e21863f4b082d669847d617bd8cf672a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6e6563656e7a757261742f656c6f7175656e742d6d6574612e7376673f7374796c653d666c61742d626c7565)](https://packagist.org/packages/necenzurat/eloquent-meta)[![Source](https://camo.githubusercontent.com/769236879b4c3e8a1cdf911a144789d3be506004e136018520364c11b8f591b1/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f736f757263652d6e6563656e7a757261742f656c6f7175656e742d2d6d6574612d626c75652e7376673f7374796c653d666c61742d737175617265)](https://github.com/necenzurat/eloquent-meta/)[![SensioLabsInsight](https://camo.githubusercontent.com/aa0c2b30151ae9db84eb3c08c63ee6851a1fd17bb64abd7340de8d5996f0cb34/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f38356431326561642d613838372d343963392d386334322d6164633036383661636135612f6d696e692e706e67)](https://insight.sensiolabs.com/projects/85d12ead-a887-49c9-8c42-adc0686aca5a)[![License](https://camo.githubusercontent.com/ea013d03889ac56fbd51b001ac91817c4dded4a82e135fb59455004279ad0d3d/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6e6563656e7a757261742f656c6f7175656e742d6d6574612e7376673f7374796c653d666c61742d737175617265)](/eloquent-meta/LICENSE)[![BADGINATOR](https://camo.githubusercontent.com/e19f2c7fd8b63b2a130e40bff41d789000ffefa1c8d86b539ee28cb2f0a33138/68747470733a2f2f62616467696e61746f722e6865726f6b756170702e636f6d2f6e6563656e7a757261742f656c6f7175656e742d6d6574612e737667)](https://github.com/defunctzombie/badginator)

A fork from [kodeine/laravel-meta](https://github.com/kodeine/laravel-meta) updated and compatible with Laravel 5.x (including 5.5)

Metable Trait adds the ability to access meta data as if it is a property on your model. Metable is Fluent, just like using an eloquent model attribute you can set or unset metas. Follow along the documentation to find out more.

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

[](#installation)

#### Composer

[](#composer)

Add this to your composer.json file, in the require object:

```
composer require necenzurat/eloquent-meta
```

After that, run composer install to install the package.

Todo: make an generator for this

#### Migration Table Schema

[](#migration-table-schema)

```
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
    Schema::create('MODEL_meta', function (Blueprint $table) {
        $table->increments('id');

        $table->integer('model_id')->unsigned()->index();
        $table->foreign('model_id')->references('id')->on('MODEL')->onDelete('cascade');

        $table->string('type')->default('null');

        $table->string('key')->index();
        $table->text('value')->nullable();

        $table->timestamps();
    });
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
    Schema::drop('MODEL_meta');
}
```

Configuration
-------------

[](#configuration)

#### Model Setup

[](#model-setup)

Next, add the `Metable` trait to each of your metable model definition:

```
use necenzurat\EloquentMeta\Metable;

class Post extends Eloquent
{
    use Metable;
}
```

Metable Trait will automatically set the meta table based on your model name. Default meta table name would be, `model_meta`. In case you need to define your own meta table name, you can specify in model:

```
class Post extends Eloquent
{
    protected $metaTable = 'model_meta'; //optional.
}
```

#### Gotcha

[](#gotcha)

When you extend a model and still want to use the same meta table you must override `getMetaKeyName` function.

```
class Post extends Eloquent
{

}

class Slideshow extends Post
{
    protected function getMetaKeyName()
    {
        return 'post_id' // The parent foreign key
    }
}

```

Working With Meta
-----------------

[](#working-with-meta)

#### Setting Content Meta

[](#setting-content-meta)

To set a meta value on an existing piece of content or create a new data:

> **Fluent way**, You can **set meta flawlessly** as you do on your regular eloquent models. Metable checks if attribute belongs to model, if not it will access meta model to append or set a new meta.

```
$post = Post::find(1);
$post->name = 'hello world'; // model attribute
$post->content = 'some content goes here'; // meta data attribute
$post->save(); // save attributes to respective tables
```

Or

```
$post = Post::find(1);
$post->name = 'hello world'; // model attribute
$post->setMeta('content', 'Some content here');
$post->save();
```

Or `set multiple metas` at once:

```
...
$post->setMeta([
    'content' => 'Some content here',
    'views' => 1,
]);
$post->save();
```

> **Note:** If a piece of content already has a meta the existing value will be updated.

#### Unsetting Content Meta

[](#unsetting-content-meta)

Similarly, you may unset meta from an existing piece of content:

> **Fluent way** to unset.

```
$post = Post::find(1);
$post->name // model attribute
unset($post->content) // delete meta on save
$post->save();
```

Or

```
$post->unsetMeta('content');
$post->save();
```

Or `unset multiple metas` at once:

```
$post->unsetMeta('content,views');
// or
$post->unsetMeta('content|views');
// or
$post->unsetMeta('content', 'views');
// or array
$post->unsetMeta(['content', 'views']);

$post->save();
```

> **Note:** The system will not throw an error if the content does not have the requested meta.

#### Checking for Metas

[](#checking-for-metas)

To see if a piece of content has a meta:

> **Fluent way**, Metable is clever enough to understand $post-&gt;content is an attribute of meta.

```
if (isset($post->content)) {

}
```

#### Retrieving Meta

[](#retrieving-meta)

To retrieve a meta value on a piece of content, use the `getMeta` method:

> **Fluent way**, You can access meta data as if it is a property on your model. Just like you do on your regular eloquent models.

```
$post = Post::find(1);
dump($post->name);
dump($post->content); // will access meta.
```

Or

```
$post = $post->getMeta('content');
```

Or specify a default value, if not set:

```
$post = $post->getMeta('content', 'Something');
```

You may also retrieve more than one meta at a time and get an illuminate collection:

```
// using comma or pipe
$post = $post->getMeta('content|views');
// or an array
$post = $post->getMeta(['content', 'views']);
```

#### Retrieving All Metas

[](#retrieving-all-metas)

To fetch all metas associated with a piece of content, use the `getMeta` without any params

```
$metas = $post->getMeta();
```

#### Retrieving an Array of All Metas

[](#retrieving-an-array-of-all-metas)

To fetch all metas associated with a piece of content and return them as an array, use the `toArray` method:

```
$metas = $post->getMeta()->toArray();
```

#### Meta Table Join

[](#meta-table-join)

When you need to filter your model based on the meta data , you can use `meta` scope in Eloquent Query Builder.

```
$post = Post::meta()
    ->where(function($query){
          $query->where('posts_meta.key', '=', 'revision')
                ->where('posts_meta.value', '=', 'draft');
    })
```

#### Eager Loading

[](#eager-loading)

When you need to retrive multiple results from your model, you can eager load `metas`

```
$post = Post::with(['meta'])->get();

```

###  Health Score

35

—

LowBetter than 80% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity22

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity72

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~242 days

Recently: every ~302 days

Total

6

Last Release

1940d ago

PHP version history (4 changes)1.0.0PHP &gt;=5.4.0

v1.0.2PHP ^5.5.9 || ^7.0

1.0.4PHP ^5.5.9 || ^7.0 || ^8.0

1.0.5PHP ^7.2.5|^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/ea4683f3cd40515a967eedbefab53f6a9f08405eb5c7052bbd0b01f7f08d9360?d=identicon)[necenzurat](/maintainers/necenzurat)

---

Top Contributors

[![necenzurat](https://avatars.githubusercontent.com/u/145449?v=4)](https://github.com/necenzurat "necenzurat (24 commits)")[![kodeine](https://avatars.githubusercontent.com/u/8620059?v=4)](https://github.com/kodeine "kodeine (16 commits)")[![AoumiS](https://avatars.githubusercontent.com/u/2528896?v=4)](https://github.com/AoumiS "AoumiS (5 commits)")[![todiadiyatmo](https://avatars.githubusercontent.com/u/2767210?v=4)](https://github.com/todiadiyatmo "todiadiyatmo (5 commits)")[![olsgreen](https://avatars.githubusercontent.com/u/1324164?v=4)](https://github.com/olsgreen "olsgreen (4 commits)")[![sumityadav](https://avatars.githubusercontent.com/u/169143?v=4)](https://github.com/sumityadav "sumityadav (2 commits)")[![ray1618](https://avatars.githubusercontent.com/u/2294555?v=4)](https://github.com/ray1618 "ray1618 (1 commits)")[![shemi](https://avatars.githubusercontent.com/u/10219407?v=4)](https://github.com/shemi "shemi (1 commits)")[![hammat](https://avatars.githubusercontent.com/u/1468343?v=4)](https://github.com/hammat "hammat (1 commits)")

---

Tags

attributeseloquentlaravemetametadataon-the-flylaraveldatamodeleloquentmetadatametameta datametaskodeinemetable

### Embed Badge

![Health badge](/badges/necenzurat-eloquent-meta/health.svg)

```
[![Health](https://phpackages.com/badges/necenzurat-eloquent-meta/health.svg)](https://phpackages.com/packages/necenzurat-eloquent-meta)
```

###  Alternatives

[kodeine/laravel-meta

Fluent Meta Data for Eloquent Models, as if it is a property on your model.

426756.0k9](/packages/kodeine-laravel-meta)[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k7.2M71](/packages/mongodb-laravel-mongodb)[tucker-eric/eloquentfilter

An Eloquent way to filter Eloquent Models

1.8k4.8M26](/packages/tucker-eric-eloquentfilter)[dyrynda/laravel-model-uuid

This package allows you to easily work with UUIDs in your Laravel models.

4802.8M8](/packages/dyrynda-laravel-model-uuid)[spiritix/lada-cache

A Redis based, automated and scalable database caching layer for Laravel

591444.8k2](/packages/spiritix-lada-cache)[mmanos/laravel-metable

A meta package for Laravel 4 models.

142.3k](/packages/mmanos-laravel-metable)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
