PHPackages                             danielefavi/laravel-metadata - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. danielefavi/laravel-metadata

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

danielefavi/laravel-metadata
============================

Laravel package for handling metadata for all your models.

1.3(4y ago)528MITPHPPHP ^7.2|^8.0

Since Apr 26Pushed 4y ago1 watchersCompare

[ Source](https://github.com/danielefavi/laravel-metadata)[ Packagist](https://packagist.org/packages/danielefavi/laravel-metadata)[ RSS](/packages/danielefavi-laravel-metadata/feed)WikiDiscussions main Synced 3d ago

READMEChangelog (9)DependenciesVersions (11)Used By (0)

Laravel Metadata - Metadata for all your models
===============================================

[](#laravel-metadata---metadata-for-all-your-models)

Laravel Metadata let you store extra data using your model without adding any extra field to your model.

```
$user->saveMeta('age', 25); // storing
$user->saveMeta('dog_name', 'Buddy'); // storing

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

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

[](#installation)

Install the package via composer:

```
composer require danielefavi/laravel-metadata
```

Then add in the `config/app.php` file the entry `DanieleFavi\Metadata\MetaServiceProvider::class,` in the `providers` section:

```
    'providers' => [
        // ...
        DanieleFavi\Metadata\MetaServiceProvider::class,
    ],
```

Then run the migration:

```
php artisan migrate
```

How to use *Laravel Metadata* in your model
-------------------------------------------

[](#how-to-use-laravel-metadata-in-your-model)

After installing the package you have to add the trait `HasMetadata` in your model.

For example

```
use DanieleFavi\Metadata\HasMetadata; // to add in your model

class User extends Authenticatable
{
    use HasFactory;
    use HasMetadata; // to add in your model

    // ...
}
```

Now your model has all the methods to handle the metadata.

Usage
-----

[](#usage)

### Saving the metadata

[](#saving-the-metadata)

Using the method `saveMeta` you can save (store or update) a metadata:

```
$user->saveMeta('phone_number', '111222333'); // storing a value
$user->saveMeta('color_preference', ['orange', 'yellow']); // storing an array
```

With `saveMetas` you can save multiple metadata at once:

```
$user->saveMetas([
    'phone_number' => '333222111',
    'color_preference' => ['red', 'green'],
    'address' => '29 Arlington Avenue',
]);
```

### Getting the metadata

[](#getting-the-metadata)

The method `getMeta` retrieve the metadata for the given key:

```
$phoneNumber = $user->getMeta('phone_number'); // the value of $phoneNumber is '111222333'

// Default value in case the metadata has not been found (default: null)
$anotherMeta = $user->getMeta('another_meta', 10);
```

You can retrieve the metadata in bulk

```
// return an array key => value with the metadata specified for the given keys
$metas = $user->getMetas(['phone_number', 'address']);
// the value of the $metas is an array key => value:
// [
//     'phone_number' => '111222333',
//     'address' => '29 Arlington Avenue'
// ]

// return an array key => value containing all metadata of the user model
$metas = $user->getMetas();
```

### Getting the meta object by key

[](#getting-the-meta-object-by-key)

If you need to get the metadata object (and not just the value as `getMeta` and `getMetas`) you can use the method `getMetaObj`:

```
$metaObj = $user->getMetaObj('address');
```

### Deleting the metadata

[](#deleting-the-metadata)

```
// delete a single metadata
$user->deleteMeta('address');

// delete multiple metadata
$user->deleteMeta(['phone_number', 'address']);

// delete all metadata
$user->deleteAllMeta();
```

### Getting the meta objects of the model

[](#getting-the-meta-objects-of-the-model)

Getting the collection of metadata attached to the model:

```
$list = $user->metas;
```

### Querying

[](#querying)

Getting all the users that have the hair color brown or pink.

```
$users = User::metaWhere('hair_color', 'brown')
            ->orMetaWhere('hair_color', 'pink')
            ->get();
```

Getting all the users with the hair color brown or pink and with a dog named Charlie:

```
$users = User::metaWhere('dog_name', 'charlie')
            ->where(function($query) {
                return $query->metaWhere('hair_color', 'brown')
                    ->orMetaWhere('hair_color', 'pink');
            })
            ->get();
```

### Advanced Metadata Query

[](#advanced-metadata-query)

You can query the metadata using `has`, `whereHas` and `with`. For example:

```
$users = User::whereHas('metas', function($query) {
    $query->where('key', 'hair_color');
    $query->where('value', json_encode('blue')); // remember to json_encode the value!!!
})->get();
```

**Important**: when doing your custom queries remember to JSON encode the metavalue because in the DB the metavalue is stored as JSON.
In the example above

Eager Loading the Metadata
--------------------------

[](#eager-loading-the-metadata)

Getting all the users with all their metadata:

```
$users = User::with('metas')->get();
```

Or lazy loading the metadata:

```
$user = User::find(1);

$user->load('metas');
```

###  Health Score

29

—

LowBetter than 60% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity63

Established project with proven stability

 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

Every ~7 days

Recently: every ~15 days

Total

10

Last Release

1780d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/31dc2af9124d4f028395fc040b9f07f31e93f9f395f37ed3bf4c10c87f708fe0?d=identicon)[DanieleFavi](/maintainers/DanieleFavi)

---

Top Contributors

[![danielefavi](https://avatars.githubusercontent.com/u/7241290?v=4)](https://github.com/danielefavi "danielefavi (13 commits)")

---

Tags

metadatametalaravel meta

### Embed Badge

![Health badge](/badges/danielefavi-laravel-metadata/health.svg)

```
[![Health](https://phpackages.com/badges/danielefavi-laravel-metadata/health.svg)](https://phpackages.com/packages/danielefavi-laravel-metadata)
```

###  Alternatives

[eusonlito/laravel-meta

A package to manage Header Meta Tags

196525.1k2](/packages/eusonlito-laravel-meta)[minime/annotations

The KISS PHP annotations library

229378.6k37](/packages/minime-annotations)[mhor/php-mediainfo

PHP wrapper around the mediainfo command

120574.8k7](/packages/mhor-php-mediainfo)[zoha/laravel-meta

a package for working with models meta

236121.7k](/packages/zoha-laravel-meta)[honeystone/laravel-seo

SEO metadata and JSON-LD package for Laravel.

34744.1k](/packages/honeystone-laravel-seo)[fabianmichael/kirby-meta

Your all-in-one powerhouse for any SEO and metadata needs imaginable.

6910.7k1](/packages/fabianmichael-kirby-meta)

PHPackages © 2026

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