PHPackages                             dragosmocrii/model-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. dragosmocrii/model-meta

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

dragosmocrii/model-meta
=======================

Save and retrieve meta settings for any models.

0.2(9y ago)020MITPHPPHP &gt;=5.6

Since Dec 13Pushed 9y ago1 watchersCompare

[ Source](https://github.com/DragosMocrii/model-meta)[ Packagist](https://packagist.org/packages/dragosmocrii/model-meta)[ RSS](/packages/dragosmocrii-model-meta/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (3)Versions (4)Used By (0)

Laravel Model Meta
==================

[](#laravel-model-meta)

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

[](#introduction)

The Model Meta package for Laravel 5.3 allows you to easily store and retrieve meta data for any models. This package is an implementation of the Property Bag pattern, which is supposed to help you deal with the situations when you need to store various model properties (meta), but adding the properties to the model is not a viable option.

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

[](#installation)

### Composer

[](#composer)

```
composer require dragosmocrii/model-meta
```

### Laravel Provider

[](#laravel-provider)

Next, you need to add the ModelMetaServiceProvider to your `providers` array in config/app.php :

```
/*
* Package Service Providers...
*/

DragoshMocrii\ModelMeta\ModelMetaServiceProvider::class,
```

### Migrate &amp; Publish resources

[](#migrate--publish-resources)

The Model Meta needs to set up its table. To do so, run `php artisan migrate`.

Next, run `php artisan vendor:publish --provider="DragoshMocrii\ModelMeta\ModelMetaServiceProvider"` to copy the vendor files to your application.

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

[](#configuration)

### Model Setup

[](#model-setup)

To add the Model Meta functionality, you need to add the `MetableFunctionality` trait to your model like so:

```
use DragoshMocrii\ModelMeta\Traits\MetableFunctionality;

class Client extends Model {
	use MetableFunctionality;

}
```

Usage
-----

[](#usage)

### Setting meta

[](#setting-meta)

`bool metaSet(string $key, mixed $value, bool $force = true)`

> If **$force** is set to **false**, the meta will be saved to DB when the parent model is saved. Otherwise, the meta will be saved instantly.

> **Note**: The **$key** needs to be a **string**, otherwise an Exception will be thrown.

#### Setting meta on a new model

[](#setting-meta-on-a-new-model)

```
$model = new MetableModel;
$model->metaSet( 'key', 'value' ); //at this time, the meta is not saved to the DB yet, because the model does not have a foreign key set yet
$model->save(); // meta will be saved when the model is saved
```

#### Setting meta on an existing model

[](#setting-meta-on-an-existing-model)

```
$model = MetableModel::findOrFail( 1 );
$model->metaSet( 'key', 'value' ); //this meta will be saved to DB instantly
```

### Setting multiple meta at once

[](#setting-multiple-meta-at-once)

`bool metaSetMany(array $values, bool $force = true)`

> If **$force** is set to **false**, the meta will be saved to DB when the parent model is saved. Otherwise, the meta will be saved instantly.

> **Note**: The **$values** parameter needs to be an associative array, where the key is a **string**. If these conditions are not met, an Exception will be thrown.

```
$model = new MetableModel;
$model->metaSetMany( [
	'key' => 'value',
	'foo' => 'bar'
] ); //at this time, the meta is not saved to the DB yet, because the model does not have a foreign key set yet
$model->save(); // meta will be saved when the model is saved
```

### Retrieving meta

[](#retrieving-meta)

`mixed metaGet(string|array $keys, null|mixed $default = null)`

> **Note**: The **$default** parameter will have effect when retrieving single meta only.

#### Getting single meta

[](#getting-single-meta)

```
$model      = MetableModel::findOrFail( 1 );
$meta_value = $model->metaGet( 'foo', 'bar' ); //returns the value of meta[foo] or 'bar' if meta[foo] does not exist
```

#### Getting multiple meta

[](#getting-multiple-meta)

```
$model = MetableModel::findOrFail( 1 );
$metas = $model->metaGet( [
	'key',
	'foo'
] ); //will return an associative array containing the values for the respective meta keys. if meta does not exist, it will be assigned with a null value
```

#### Getting all meta

[](#getting-all-meta)

`mixed metaAll()`

```
$model    = MetableModel::findOrFail( 1 );
$all_meta = $model->metaAll(); //returns an array containing all meta for the current model
```

### Removing meta

[](#removing-meta)

`metaRemove(string|array $keys)`

> **Note**: Meta will be removed from the DB instantly, unless the model is missing the foreign key.

#### Removing single meta

[](#removing-single-meta)

```
$model = MetableModel::findOrFail( 1 );
$model->metaRemove( 'key' );
```

#### Removing multiple meta

[](#removing-multiple-meta)

```
$model = MetableModel::findOrFail( 1 );
$model->metaRemove( [ 'key', 'foo' ] );
```

### Check for meta

[](#check-for-meta)

`bool metaExists(string|array $keys, bool $return_missing = false)`

> If **$return\_missing** is **true**, this will return an array containing the keys of missing meta. An empty array will be returned if no meta is missing.

#### Check for single meta

[](#check-for-single-meta)

```
$model       = MetableModel::findOrFail( 1 );
$meta_exists = $model->metaExists( 'key' );
```

#### Check for multiple meta

[](#check-for-multiple-meta)

```
$model       = MetableModel::findOrFail( 1 );
$meta_exists = $model->metaExists( [
	'key',
	'foo'
] ); //return true/false if $return_missing is false. Otherwise, returns an array containing the keys of the missing meta
```

Morph Map
---------

[](#morph-map)

By default, Laravel will use the fully qualified class name to store the type of the related model. If you would like to decouple the application internal structure from the database, you should define a morph map. Thus, if you change the model's class name, or extend the model and don't want to lose the related meta data, you would simply have to change the morph map.

To change the morph map, you need to edit the `model_meta.morph_map` configuration setting, for example:

```
'morph_map' => [
    'posts' => App\Post::class,
    'videos' => App\Video::class,
]
```

Preload all meta with single meta fetching
------------------------------------------

[](#preload-all-meta-with-single-meta-fetching)

Sometimes you may call metaGet multiple times in your code. To avoid executing a DB query every time, you can tell Model Meta to preload all model meta when a single meta is retrieved, so that getting subsequent single metas will use the cache instead.

By default, Model Meta will preload the meta. If you would like to disable this, you can set the configuration `model_meta.preload_on_get` to `false`.

Model Meta &amp; Laravel
------------------------

[](#model-meta--laravel)

The model meta uses polymorphic relationships to achieve its functionality. You can keep this in mind if you need to build complex queries.

For example, you can fetch meta for a model, using default Laravel methods:

```
$model       = MetableModel::findOrFail( 1 );
$metas       = $model->meta()->get()->toArray();
$single_meta = $model->meta()->where( 'key', 'keyname' )->get()->toArray();
```

Known issues
------------

[](#known-issues)

- Setting metas will do 2 queries for each meta. This is because Model Meta uses the `updateOrCreate` Laravel method, which executes 2 queries against the DB.
- At each meta retrieval, a DB query will be executed. Will fix this by eager loading the meta on the model.

###  Health Score

23

—

LowBetter than 26% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity50

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

Every ~1 days

Total

3

Last Release

3485d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1543960?v=4)[Dragoș Mocrii](/maintainers/dragosmocrii)[@DragosMocrii](https://github.com/DragosMocrii)

---

Top Contributors

[![DragosMocrii](https://avatars.githubusercontent.com/u/1543960?v=4)](https://github.com/DragosMocrii "DragosMocrii (10 commits)")

---

Tags

laravelmeta datapost metamodel settingsmodel metaproperty-bagmodel-options

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/dragosmocrii-model-meta/health.svg)

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

###  Alternatives

[tpetry/laravel-postgresql-enhanced

Support for many missing PostgreSQL specific features

1.0k2.3M27](/packages/tpetry-laravel-postgresql-enhanced)[anourvalar/eloquent-serialize

Laravel Query Builder (Eloquent) serialization

11222.5M33](/packages/anourvalar-eloquent-serialize)[statamic-rad-pack/runway

Eloquently manage your database models in Statamic.

135212.4k7](/packages/statamic-rad-pack-runway)[waad/laravel-model-metadata

A robust Laravel package for handling metadata with JSON casting, custom relation names, and advanced querying capabilities.

854.1k](/packages/waad-laravel-model-metadata)[mozex/laravel-scout-bulk-actions

Import, flush, and queue-import all your Laravel Scout searchable models at once. Auto-discovers models, runs in bulk, tracks progress.

1437.7k](/packages/mozex-laravel-scout-bulk-actions)[ramadan/easy-model

A Laravel package for enjoyably managing database queries.

111.6k](/packages/ramadan-easy-model)

PHPackages © 2026

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