Simple for adding tags to eloquent models.

1.2.0 2017-02-04 02:06 UTC

This package is not auto-updated.

Last update: 2020-08-07 20:14:43 UTC


It is a tag package that allows user to easily add tags to the Eloquent entities.

If you are a Chinese user, you can see this documentation. 点击此处



Run the following command from your terminal:

composer require "fuguevit/tags: ^1.0.0"

or add this to require section in your composer.json file:

"fuguevit/tags": "^1.0.0"

then run composer update


Add the Fuguevit\Tags\Providers\TagsServiceProvider Service Provider into the providers array on your config/app.php file and run the following on your terminal:

php artisan vendor:publish --provider="Fuguevit\Tags\Providers\TagsServiceProvider" --tag="config"
php artisan vendor:publish --provider="Fuguevit\Tags\Providers\TagsServiceProvider" --tag="migrations"


You can easily reset your Eloquent model like below:

Use the Fuguevit\Tags\Traits\TaggableTrait and implement the Fuguevit\Tags\Contracts\TaggableInterface interface.

    use Fuguevit\Tags\Contracts\TaggableInterface;
    use Fuguevit\Tags\Traits\TaggableTrait;
    class User extends Model implements TaggableInterface
        use TaggableTrait;
        // orign content ...

Then you can use :

$user->tag("foo,bar,laravel");              // add three tags named foo,bar,laravel 
$user->tag(["foo","bar","laravel"]);        // the same
$user->untag("foo);                         // untag foo
$user->untag();                             // untag all
$user->tags;                                // all tags morphed

Total methods will be described in next section.


The following methods are available:


public static function allTags();
public function tags();
public function tag($tags);
public function untag($tags = null);
public function setTags($tags, $type = 'name');
public static function scopeWhereTag(Builder $query, $tags, $type = 'slug');
public static function scopeWithTag(Builder $query, $tags, $type = 'slug');
public static function scopeWithoutTag(Builder $query, $tags, $type = 'slug');


  • Get all tags from an entity namespace , imagine User
$allTags = User::allTags()->get();
  • Get all tags from an entity, imagine $user
$tags = $user->tags()->get();
  • Add tag(s) to an entity
// use string as parameter
// or an array
  • Remove tag(s) from an entity
// you can use array as parameter, too
// remove all tags from an entity
  • Add and remove original tags from an entity to make it same as you providers
// you can use array as parameter

If you want to get all entities that contain all tags you provide, you can


Elseif you want to get all entities that contain at least one tag you provide


If you want to get entities that does not have all tags you provide