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

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

mmanos/laravel-taggable
=======================

A tagging package for Laravel 4 models.

v1.0.1(11y ago)541MITPHPPHP &gt;=5.4.0

Since Jan 25Pushed 11y ago2 watchersCompare

[ Source](https://github.com/mmanos/laravel-taggable)[ Packagist](https://packagist.org/packages/mmanos/laravel-taggable)[ RSS](/packages/mmanos-laravel-taggable/feed)WikiDiscussions master Synced today

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

Tagging Package for Laravel 4
=============================

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

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

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

[](#installation)

#### Composer

[](#composer)

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

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

After that, run composer install to install the package.

#### Service Provider

[](#service-provider)

Register the `Mmanos\Taggable\TaggableServiceProvider` in your `app` configuration file.

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

[](#configuration)

#### Tags Migration and Model

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

First you'll need to publish a `tags` table and a `Tag` model. This table will hold a summary of all tags created by your taggable models.

```
$ php artisan laravel-taggable:tags tags
```

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

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

#### Taggable Migration

[](#taggable-migration)

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

```
$ php artisan laravel-taggable:taggable user_tags
```

#### Run Migrations

[](#run-migrations)

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

#### Model Setup

[](#model-setup)

Next, add the `Taggable` trait to each taggable model definition:

```
use Mmanos\Taggable\Taggable;

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

Then you need to specify the tag model as well as the taggable table to use with your model:

```
class User extends Eloquent
{
	protected $tag_model = 'Tag';
	protected $taggable_table = 'user_tags';
}
```

#### Syncing Custom Attributes

[](#syncing-custom-attributes)

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

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

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

```
class User extends Eloquent
{
	protected $taggable_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 taggable table records for the piece of content.

#### Syncing Deleted Content

[](#syncing-deleted-content)

This package will automatically delete all taggable 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 taggable table records, this package will automatically soft-delete all taggable table records for a piece of content when that piece of content is deleted. If the content is restored, then the taggable table records are restored as well.

Working With Tags
-----------------

[](#working-with-tags)

#### Tagging Content

[](#tagging-content)

To add a tag to an existing piece of content:

```
$user->tag('Frequent Visitor');
```

Or add multiple tags at once:

```
$user->tag('Frequent Visitor', 'Happy');
// or
$user->tag(['Frequent Visitor', 'Happy']);
```

> **Note:** If a piece of content already has a tag it will not be added a second time and will not throw an error.

#### Removing Tags

[](#removing-tags)

Similarly, you may remove tags from an existing piece of content:

```
$user->untag('Frequent Visitor');
```

Or remove multiple tags at once:

```
$user->untag('Frequent Visitor', 'Happy');
// or
$user->untag(['Frequent Visitor', 'Happy']);
```

> **Note:** The system will not throw an error if the content does not have the requested tag.

#### Checking for Tags

[](#checking-for-tags)

To see if a piece of content has a tag:

```
if ($user->hasTag('Frequent Visitor')) {

}
```

#### Retrieving All Tags

[](#retrieving-all-tags)

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

```
$tags = $user->tags;
```

#### Retrieving an Array of All Tags

[](#retrieving-an-array-of-all-tags)

To fetch all tags associated with a piece of content and return them as an array, use the `tagsArray` method:

```
$tags = $user->tagsArray();
```

Querying for Tagged Content
---------------------------

[](#querying-for-tagged-content)

#### Performing Queries

[](#performing-queries)

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

```
$users = User::withTag('Frequent Visitor')->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::withTag('Frequent Visitor')
	->where('tag_created_at', '>', '2015-01-01 00:00:00')
	->with('company')
	->orderBy('tag_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 more than one tag:

```
$users = User::withTag('Frequent Visitor', 'Happy')->get();
// or
$users = User::withTag(['Frequent Visitor', 'Happy'])->get();
```

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

```
$users = User::withAnyTag('Frequent Visitor', 'Happy')->get();
// or
$users = User::withAnyTag(['Frequent Visitor', 'Happy'])->get();
```

> **Note:** Query performance can be reduced for these types of queries if your queries match thousands of records or more.

And you may combine multiple filters:

```
// Fetch all users who have the 'Agent' tag and who have 'Frequent Visitor' or 'Happy'.
$users = User::withTag('Agent')->withAnyTag('Frequent Visitor', 'Happy')->get();
```

#### Tag Contexts

[](#tag-contexts)

Sometimes you might want to associate your tags (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 tag record with a company allowing you to fetch all tags 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 tags 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 taggable model by adding a `tagContext` method:

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

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

```
class Tag 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 tag queries used by this package to filter on `company_id`.

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

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

```
$users = User::withTag('Frequent Visitor')->withTagContext($company)->take(10)->get();
```

###  Health Score

28

—

LowBetter than 52% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 83.3% 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 ~161 days

Total

2

Last Release

4016d ago

### Community

Maintainers

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

---

Top Contributors

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

---

Tags

laravelmodeleloquenttagstagtaggingTaggable

### Embed Badge

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

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

###  Alternatives

[rtconner/laravel-tagging

Use PHP traits to extend Laravel Eloquent Models to allow Tags. Models can be marked as Taggable.

8853.2M15](/packages/rtconner-laravel-tagging)[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k8.4M96](/packages/mongodb-laravel-mongodb)[cviebrock/eloquent-taggable

Easy ability to tag your Eloquent models in Laravel.

574723.0k4](/packages/cviebrock-eloquent-taggable)[lemaur/eloquent-publishing

218.1k1](/packages/lemaur-eloquent-publishing)

PHPackages © 2026

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