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

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

mmanos/laravel-metable
======================

A meta package for Laravel 4 models.

v1.0.2(10y ago)142.3k3[1 issues](https://github.com/mmanos/laravel-metable/issues)MITPHPPHP &gt;=5.4.0

Since Jan 25Pushed 9y ago3 watchersCompare

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

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

Meta Package for Laravel 4
==========================

[](#meta-package-for-laravel-4)

This package adds meta support to your Laravel application. You can configure it to attach meta to any of your existing Eloquent models.

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

[](#installation)

#### Composer

[](#composer)

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

```
"mmanos/laravel-metable": "dev-master"
```

After that, run composer install to install the package.

#### Service Provider

[](#service-provider)

Register the `Mmanos\Metable\MetableServiceProvider` in your `app` configuration file.

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

[](#configuration)

#### Metas Migration and Model

[](#metas-migration-and-model)

First you'll need to publish a `metas` table and a `Meta` model. This table will hold a summary of all meta created by your metable models.

```
$ php artisan laravel-metable:metas metas
```

> **Note:** Modify the last parameter of this call to change the table/model name.

> **Note:** You may publish as many meta tables as you need, if you want to keep the meta separate for different types of content, for example.

#### Metable Migration

[](#metable-migration)

Next, publish a migration for each type of content you want to attach meta to. You may attach meta to as many types of content as you wish. For example, if you want to be able to add meta to both a `users` table and a `blog_posts` table, run this migration once for each table.

```
$ php artisan laravel-metable:metable user_metas
```

#### Run Migrations

[](#run-migrations)

Once the migration has been created, simply run the `migrate` command.

#### Model Setup

[](#model-setup)

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

```
use Mmanos\Metable\Metable;

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

Then you need to specify the meta model as well as the metable table to use with your model:

```
class User extends Eloquent
{
	protected $meta_model = 'Meta';
	protected $metable_table = 'user_metas';
}
```

#### Syncing Custom Attributes

[](#syncing-custom-attributes)

Sometimes you will want to have some of the same fields in your content table synced to the metable table records. This will allow you to filter and sort by these attributes when querying the metable table. Luckily this system will automatically sync any fields you define to the metable table records any time there are changes.

To get started, **modify the metable migration file** to include your additional fields.

Then, tell your model which fields it needs to sync:

```
class User extends Eloquent
{
	protected $metable_table_sync = ['company_id', 'created_at', 'updated_at', 'deleted_at'];
}
```

Now every time you create or update a model, these fields will by synced to all metable table records for the piece of content.

#### Syncing Deleted Content

[](#syncing-deleted-content)

This package will automatically delete all metable table records for a piece of content when that piece of content is deleted.

If you are using the `SoftDeletingTrait` and you are syncing the `deleted_at` column to your metable table records, this package will automatically soft-delete all metable table records for a piece of content when that piece of content is deleted. If the content is restored, then the metable table records are restored as well.

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

[](#working-with-meta)

#### Setting Content Meta

[](#setting-content-meta)

To set a meta value on an existing piece of content:

```
$user->setMeta('employer', 'Company, Inc.');
```

Or set multiple metas at once:

```
$user->setMeta([
	'employer' => 'Company, Inc.',
	'employed_for_years' => 10,
]);
```

> **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:

```
$user->unsetMeta('employer');
```

Or unset multiple metas at once:

```
$user->unsetMeta('employer', 'employed_for_years');
// or
$user->unsetMeta(['employer', 'employed_for_years']);
```

> **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:

```
if ($user->hasMeta('employer')) {

}
```

#### Retrieving Meta

[](#retrieving-meta)

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

```
$employer = $user->meta('employer');
```

Or specify a default value, if not set:

```
$employer = $user->meta('employer', 'Unemployed');
```

You may also retrieve more than one meta at a time and get back an array of values:

```
$employer = $user->meta(['employer', 'employed_for_years']);
```

#### Retrieving All Metas

[](#retrieving-all-metas)

To fetch all metas associated with a piece of content, use the `metas` relationship:

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

#### 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 `metasArray` method:

```
$metas = $user->metasArray();
```

Querying for Content from Meta
------------------------------

[](#querying-for-content-from-meta)

#### Performing Queries

[](#performing-queries)

Now let's say you want to query for all content that has a given meta:

```
$users = User::withMeta('employer')->take(10)->get();
```

Or optionally specify the meta value to match:

```
$users = User::whereMeta('employer', 'Company, Inc.')->take(10)->get();
```

Or optionally specify the meta value and operator to match:

```
$users = User::whereMeta('employed_for_years', '>', '5')->take(10)->get();
```

These queries extend the same `QueryBuilder` class that you are used to working with, so all of those methods work as well:

```
$users = User::whereMeta('employer', 'Company, Inc.')
	->where('meta_created_at', '>', '2015-01-01 00:00:00')
	->with('company')
	->orderBy('meta_created_at', 'desc')
	->paginate(10);
```

> **Note:** The `update` and `delete` methods on a QueryBuilder object do not work for these queries.

You may query for content that has any of the given meta:

```
$users = User::withAnyMeta('company', 'employed_for_years')->get();
// or
$users = User::withAnyMeta(['company', 'employed_for_years'])->get();
```

Or query for content that has any of the given meta that matches the given values:

```
$users = User::whereAnyMeta([
	'company' => 'Company, Inc.',
	'employed_for_years' => '10'
])->get();
```

> **Note:** Query performance can be reduced for the `withAnyMeta` and `whereAnyMeta` queries if your queries match thousands of records or more.

And you may combine multiple filters:

```
// Fetch all users who have the 'agent' meta and who have 'company' or 'employed_for_years'.
$users = User::whereMeta('agent', '1')->withAnyMeta('company', 'employed_for_years')->get();
```

#### Meta Contexts

[](#meta-contexts)

Sometimes you might want to associate your metas (summary) table records with some custom context for your application. For example, say you have a `companies` table and a `users` table and each user belongs to a company. And now you also want to associate each meta record with a company allowing you to fetch all meta used by each individual company. In order to do so, we have to tell this package to be aware of this company context and modify it's queries accordingly.

To get started, make sure you **modify your metas migration** to include any context fields (`company_id`, in this case). You might also need to update the unique index, if necessary.

Then modify your metable model by adding a `metaContext` method:

```
class User extends Eloquent
{
	public function metaContext()
	{
		return $this->company;
	}
}
```

Next modify your `Meta` model (or whatever name you specified during configuration) to apply any contexts:

```
class Meta extends Eloquent
{
	public static function applyQueryContext($query, $context)
	{
		$query->where('company_id', $context->id);
	}

	public static function applyModelContext($model, $context)
	{
		$model->company_id = $context->id;
	}
}
```

The `applyQueryContext` method will adjust any meta queries used by this package to filter on `company_id`.

The `applyModelContext` method is called when creating a new `Meta` record and should set any required context fields.

Finally, when performing queries, specify the context to apply:

```
$users = User::withMeta('employer')->withMetaContext($company)->take(10)->get();
```

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity25

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 50% 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 ~80 days

Total

3

Last Release

3964d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/972055?v=4)[Mark Manos](/maintainers/mmanos)[@mmanos](https://github.com/mmanos)

---

Top Contributors

[![dmyers](https://avatars.githubusercontent.com/u/207171?v=4)](https://github.com/dmyers "dmyers (3 commits)")[![mmanos](https://avatars.githubusercontent.com/u/972055?v=4)](https://github.com/mmanos "mmanos (3 commits)")

---

Tags

laravelmodeleloquentmetametasmetable

### Embed Badge

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

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

###  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)[pdphilip/elasticsearch

An Elasticsearch implementation of Laravel's Eloquent ORM

145360.2k4](/packages/pdphilip-elasticsearch)

PHPackages © 2026

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