PHPackages                             marshmallow/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. marshmallow/metadata

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

marshmallow/metadata
====================

A package to simply add metadata to models

v1.5.0(3mo ago)04.6k↓63.3%11MITPHPPHP ^8.0CI passing

Since Nov 7Pushed 3w ago1 watchersCompare

[ Source](https://github.com/marshmallow-packages/metadata)[ Packagist](https://packagist.org/packages/marshmallow/metadata)[ Docs](https://github.com/marshmallow-packages/metadata)[ RSS](/packages/marshmallow-metadata/feed)WikiDiscussions main Synced yesterday

READMEChangelog (10)Dependencies (4)Versions (17)Used By (1)

[![alt text](https://camo.githubusercontent.com/f5450f299f5713ce2f04dd5a1ba7ce9960ed4568b3574e4c4ee3cddc75477253/68747470733a2f2f6d617273686d616c6c6f772e6465762f63646e2f6d656469612f6c6f676f2d7265642d3233377834362e706e67 "marshmallow.")](https://camo.githubusercontent.com/f5450f299f5713ce2f04dd5a1ba7ce9960ed4568b3574e4c4ee3cddc75477253/68747470733a2f2f6d617273686d616c6c6f772e6465762f63646e2f6d656469612f6c6f676f2d7265642d3233377834362e706e67)

Metadata
========

[](#metadata)

[![Latest Version on Packagist](https://camo.githubusercontent.com/477597d35c25e9b8b4304d30fbed1fd3affa8d8771fa4966fd2c196d2930dd54/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d617273686d616c6c6f772f6d657461646174612e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/marshmallow/metadata)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/c994e7373479df77295b597a876d623f591d81b6e39eb3930ae32c6b293b393a/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6d617273686d616c6c6f772d7061636b616765732f6d657461646174612f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/marshmallow-packages/metadata/actions/workflows/fix-php-code-style-issues.yml)[![Total Downloads](https://camo.githubusercontent.com/31146844c7430788cf086daa33f6a2b9cce969d930c7449f4f681239cd1bbbe2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d617273686d616c6c6f772f6d657461646174612e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/marshmallow/metadata)

A package to simply add metadata to any Eloquent model with a simple cast. Metadata is stored in a single related table as JSON, keyed per model, so you can attach arbitrary attributes to a model without adding extra columns.

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

[](#installation)

You can install the package via composer:

```
composer require marshmallow/metadata
```

You can publish and run the migrations with:

```
php artisan vendor:publish --tag="metadata-migrations"
php artisan migrate
```

You can publish the config file with:

```
php artisan vendor:publish --tag="metadata-config"
```

This is the contents of the published config file:

```
return [
    /*
     * The fully qualified class name of the metadata model.
     */
    'metadata_model' => Marshmallow\Metadata\Models\Metadata::class,

    /*
     * The fully qualified class name of the metadata cast.
     */
    'metadata_cast' => Marshmallow\Metadata\Casts\MetadataCast::class,

    /*
     * Automatically eager load metadata to prevent N+1 queries.
     * Set to false if you prefer to manually eager load metadata.
     */
    'eager_load' => false,
];
```

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

[](#configuration)

KeyDefaultDescription`metadata_model``Marshmallow\Metadata\Models\Metadata::class`The model used to store metadata. Swap it to use your own.`metadata_cast``Marshmallow\Metadata\Casts\MetadataCast::class`The cast that maps a model attribute to metadata storage.`eager_load``false`When `true`, the `metadata` relation is automatically eager loaded on models using the trait to prevent N+1 queries.Usage
-----

[](#usage)

Add the `HasMetadata` trait to your model:

```
use Marshmallow\Metadata\Traits\HasMetadata;

class Example extends Model
{
    use HasMetadata;
}
```

Add a cast for every field you want to store as metadata:

```
use Marshmallow\Metadata\Casts\MetadataCast;

protected $casts = [
    'example_field' => MetadataCast::class,
];
```

After which you can get &amp; set your field using normal model attribute access:

```
$example_model->example_field = 'This is an example';
$example_model->save();

$example_field = $example_model->example_field;
// 'This is an example'
```

Array and object values are automatically encoded to JSON on write and decoded on read:

```
$example_model->example_field = ['key' => 'value'];

$example_model->example_field;
// ['key' => 'value']
```

When you set metadata on a model that has not been persisted yet, the value is queued and written automatically once the model is created. Metadata is also automatically deleted when the model itself is deleted.

### Working with the metadata relation directly

[](#working-with-the-metadata-relation-directly)

Each model that uses the trait exposes a `metadata` morph-one relation backed by the `Metadata` model, which offers a few helpers:

```
// Read or write a single key
$example_model->metadata->getMetadata('example_field');
$example_model->metadata->addMetadata('example_field', 'value');

// Remove a single key
$example_model->metadata->removeKey('example_field');

// Clear all metadata for the model
$example_model->metadata->clear();
```

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please report security vulnerabilities by email rather than via the public issue tracker.

Credits
-------

[](#credits)

- [Marshmallow](https://github.com/marshmallow-packages)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

49

—

FairBetter than 94% of packages

Maintenance89

Actively maintained with recent releases

Popularity22

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity60

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 ~111 days

Recently: every ~122 days

Total

12

Last Release

107d ago

Major Versions

v0.0.1 → v1.0.02022-11-07

PHP version history (3 changes)v0.0.1PHP ^8.1

v1.0.1PHP ^8.0|^8.1

v1.1.0PHP ^8.0

### Community

Maintainers

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

---

Top Contributors

[![stefvanesch](https://avatars.githubusercontent.com/u/46725619?v=4)](https://github.com/stefvanesch "stefvanesch (25 commits)")[![LTKort](https://avatars.githubusercontent.com/u/2412670?v=4)](https://github.com/LTKort "LTKort (22 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (19 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (10 commits)")

---

Tags

laravelmetadatamarshmallow

### Embed Badge

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

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

###  Alternatives

[spatie/laravel-pdf

Create PDFs in Laravel apps

1.0k4.8M47](/packages/spatie-laravel-pdf)[codewithdennis/filament-select-tree

The multi-level select field enables you to make single selections from a predefined list of options that are organized into multiple levels or depths.

329530.5k29](/packages/codewithdennis-filament-select-tree)[worksome/exchange

Check Exchange Rates for any currency in Laravel.

124603.0k](/packages/worksome-exchange)[rawilk/profile-filament-plugin

Profile &amp; MFA starter kit for filament.

3914.6k](/packages/rawilk-profile-filament-plugin)[tarfin-labs/event-machine

Event-driven state machines for Laravel with event sourcing, type-safe context, and full audit trail.

199.4k](/packages/tarfin-labs-event-machine)[tapp/filament-form-builder

User facing form builder using Filament components

132.4k3](/packages/tapp-filament-form-builder)

PHPackages © 2026

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