PHPackages                             elhareth/laravel-eloquent-metable - 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. elhareth/laravel-eloquent-metable

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

elhareth/laravel-eloquent-metable
=================================

A Laravel package to extend Eloquent models data

v1.0.0(2y ago)021MITPHPPHP ^8.0

Since Nov 13Pushed 2y ago1 watchersCompare

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

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

Laravel-Eloquent-Metable
========================

[](#laravel-eloquent-metable)

Laravel-Eloquent-Metable is a package for extending laravel Eloquent models without messing up with the database schema.

Example Usage
-------------

[](#example-usage)

Adding metadata to an eloquent model

```
$user = User::create([
    'name' => 'User Name',
    'email' => 'username@email.com',
    'password' => 'password',
]);
$user->setMeta('gender', 'male');
```

Query scope to fetch model by it's metadata

```
$user = User::whereMeta('gender', 'male');
```

Get attached metadata

```
$gender = $user->getMeta('gender');
```

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

[](#installation)

Add the package to your Laravel app using composer

```
composer require elhareth/laravel-eloquent-metable
```

Run the migrations to add the required table to your database.

```
php artisan migrate
```

Add the `Elhareth\LaravelEloquentMetable\IsMetable` trait to any eloquent model class that you want to be able to attach metadata to.

```
use Illuminate\Database\Eloquent\Model;
use Elhareth\LaravelEloquentMetable\IsMetable;

class User extends Model
{
    use IsMetable;
}
```

Documentation
-------------

[](#documentation)

### Handling Meta

[](#handling-meta)

The `IsMetable` trait will add `metalist()` method to your model, which meant to handle the realtion between your model and the meta data model `Metable`.

#### Attaching MetaData

[](#attaching-metadata)

Attach meta to a model with the `setMeta()` method. The method accepts three arguments: a string to use as a meta name, a value and additionally a string for group. The value argument will accept a number of different inputs, which will be converted to a string for storage in the database.

```
 $user->setMeta('key', 'value');
 $user->setMeta('key', 'value', null);
 $user->setMeta('key', 'value', 'group');
```

If you want to add many meta records you may pass an array as first parameter

```
 $user->setMeta([
    'full_name' => 'Full Name',
    'gender' => 'male',
    'bio' => 'Lorem absum...',
 ]);
```

Also you pass a second param as meta data group

```
 $user->setMeta([
    'full_name' => 'Full Name',
    'gender' => 'male',
    'bio' => 'Lorem absum...',
 ], 'profile');
```

#### Be Carefull !

[](#be-carefull-)

if you adding a meta record with value of type array if it has value/group key it will be treated as `$value` / `$group` params, so when you adding an array that may have these keys consider using `setMeta()` method where the value param is treated as value no matter what it does contain.

```
 $user->setMeta('key', [
    'value' => 'random-content',
 ]); // 'a:1:{s:5:"value";s:14:"random-content";};'
 $value = $user->getMeta('key'); // ['value' => 'random-content']
```

#### Retrieving MetaData

[](#retrieving-metadata)

You can retrieve the value of the meta at a given key with the `getMeta()` method.

```
 $gender = $user->getMeta('gender');
```

The method `getMeta` accepts three arguments; `getMeta(name: $name, default: $default nullable $nullable)`The first the name of meta record The second parameter to specify a default value to return if no meta has been set at that key. The third parameter is a boolean to set whether to treat null values as not found

```
 $user->getMeta('gender', 'male'); // will return 'male' if not set
 $user->getMeta('gender', 'male', false); // will return male if not set or if value is null
 $user->getMeta('gender', 'male', true); // will return male if not set or null if is set to null
```

You may retrieve a collection of meta depending on specifec group

```
 $profile = $user->getMetaGroup('profile'); // return collection
```

#### Checking For Presence of MetaData

[](#checking-for-presence-of-metadata)

You can check if a value has been assigned to a given key with the `hasMeta()` method. This method will return true if a record is found on database. You may pass another boolean value to control how to treat null values.

```
 if ($user->hasMeta('gender')) {
    // ...
 }

 if ($user->has(name: 'gender', nullable: true)) {
    // ...
 }
```

#### Queued Metables

[](#queued-metables)

You may mass assign metables and they will be attached once the model is created

```
 $user = User::create([
    'name' => 'UserName',
    'email' => 'username@email.com',
    'password' => 'password',
    'metables' => [
        'gender' => 'male', // OR
        'gender' => [
            'value' => 'male', // OR
        ],
        'gender' => [
            'value' => 'male',
            'group' => 'profile', // If not specified group is null
        ],
    ],
 ]);

 $user->getMeta('gender') // male
```

### Pre Metables

[](#pre-metables)

You may define default metables for each model and they'll be added once the model is created.

To do so you should implement `Elhareth\LaravelEloquentMetable\IsMetableInterface`&gt; And define a method `defaultMetables()` which returns an array of metables you want to automatically attach to your model.

```
 use Elhareth\LaravelEloquentMetable\IsMetableInterface;
 use Illuminate\Database\Eloquent\Model;

 class User extends Model implements IsMetableInterface
 {
    /**
     * Default Metables
     *
     * @return array
     */
    protected function defaultMetables(): array
    {
        return [
            'gender' => [
                'value' => null,
                'group' => 'profile',
            ],
            'bio' => [
                'value' => null,
                'group' => 'profile',
            ],
            'locale' => [
                'value' => 'en',
                'group' => null,
            ],
            'avatar' => [
                'value' => 'avatar.png',
            ],
            'theme' => 'dark',
        ];
    }
 }
```

You can get list of model default metables by `getDefaultMetables()`.

#### Deleting MetaData

[](#deleting-metadata)

To remove the meta stored at a given key, use `removeMeta()`.

```
 $user->removeMeta('bio');
```

To delete all meta records from a model, use `deleteMetaRecords()`.

```
 $user->deleteMetaRecords();
```

Attached meta is automatically purged from the database when a `IsMetable` model is manually deleted. Metaable will `not` be cascaded if the model is deleted by the query builder.

```
 $user->delete(); // will delete attached meta
 User::where(...)->delete() // will NOT delete attached meta
```

#### Eager Loading MetaData

[](#eager-loading-metadata)

When working with collections of `IsMetable` models, be sure to eager load the meta relation for all instances together to avoid repeated database queries (i.e. N+1 problem).

Eager load from the query builder:

```
 $users = User::with('metalist')->where(...)->get();
```

Lazy eager load from an Eloquent collection:

```
 $users->load('metalist');
```

You can also instruct your model class to `always` eager load the meta relationship by adding `'metalist'` to your model's `$with` property.

```
 class User extends Model {
     use IsMetable;

     protected $with = ['metalist'];
 }
```

### Querying Meta

[](#querying-meta)

The `IsMetable` trait provides a number of query scopes to facilitate modifying queries based on the meta attached to your models

#### Checking for Presence of a key

[](#checking-for-presence-of-a-key)

To only return records that have a value assigned to a particular key, you can use `whereHasMeta()`. You can also pass an array to this method, which will cause the query to return any models attached to one or more of the provided keys.

```
 $users = User::whereHasMeta('gender')->get();
 $users = User::whereHasMeta(['gender', 'bio'])->get();
```

You can also query for records that does not contain a meta key using the `whereDoesntHaveMeta()`. Its signature is identical to that of `whereHasMeta()`.

```
 $users = User::whereDoesntHaveMeta('gender')->get();
 $users = User::whereDoesntHaveMeta(['gender', 'bio'])->get();
```

You can restrict your query based on the value stored at a meta key. The `whereMeta()` method can be used to compare the value using any of the operators accepted by the Laravel query builder's `where()` method.

```
 $users = User::whereMeta('letters', ['a', 'b', 'c'])->get();
```

The `whereMetaIn()` method is also available to find records where the value is matches one of a predefined set of options.

```
 $users = User::whereMetaIn('country', ['CAN', 'USA', 'MEX']);
```

License
-------

[](#license)

This package is released under the MIT license (MIT).

###  Health Score

22

—

LowBetter than 22% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity47

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

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

Unknown

Total

1

Last Release

912d ago

### Community

Maintainers

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

---

Top Contributors

[![elhareth](https://avatars.githubusercontent.com/u/13013541?v=4)](https://github.com/elhareth "elhareth (14 commits)")

---

Tags

laraveleloquentmetadatalaravel-metableelhareth

### Embed Badge

![Health badge](/badges/elhareth-laravel-eloquent-metable/health.svg)

```
[![Health](https://phpackages.com/badges/elhareth-laravel-eloquent-metable/health.svg)](https://phpackages.com/packages/elhareth-laravel-eloquent-metable)
```

###  Alternatives

[illuminate/database

The Illuminate Database package.

2.8k52.4M9.4k](/packages/illuminate-database)[watson/validating

Eloquent model validating trait.

9723.3M47](/packages/watson-validating)[silber/bouncer

Eloquent roles and abilities.

3.6k4.4M25](/packages/silber-bouncer)[dyrynda/laravel-model-uuid

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

4802.8M8](/packages/dyrynda-laravel-model-uuid)[tucker-eric/eloquentfilter

An Eloquent way to filter Eloquent Models

1.8k4.8M26](/packages/tucker-eric-eloquentfilter)[reedware/laravel-relation-joins

Adds the ability to join on a relationship by name.

2121.2M13](/packages/reedware-laravel-relation-joins)

PHPackages © 2026

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