How to make your Eloquent model taggable.

This package offers taggable behavior for your models. After the package is installed the only thing you have to do is to add the HasTags trait to an Eloquent model to make it taggable.

You can use Laravel Tags if you want to add tags to your eloquent models. Here are some examples:

//create a model with some tags
$newsItem = NewsItem::create([
   'name' => 'testModel',
   'tags' => ['tag', 'tag2'], //tags will be created if they don't exist
]);

//attaching tags
$newsItem->attachTag('tag3');
$newsItem->attachTags(['tag4', 'tag5']);

//detaching tags
$newsItem->detachTags('tag3');
$newsItem->detachTags(['tag4', 'tag5']);

//syncing tags
$newsItem->syncTags(['tag1', 'tag2']); // all other tags on this model will be detached

//syncing tags with a type
$newsItem->syncTagsWithType(['tag1', 'tag2'], 'typeA'); 
$newsItem->syncTagsWithType(['tag1', 'tag2'], 'typeB'); 

//retrieving tags with a type
$newsItem->tagsWithType('typeA'); 
$newsItem->tagsWithType('typeB'); 

//retrieving models that have any of the given tags
NewsItem::withAnyTags(['tag1', 'tag2'])->get();

//retrieve models that have all of the given tags
NewsItem::withAllTags(['tag1', 'tag2'])->get();

//translating a tag
$tag = Tag::findOrCreate('my tag');
$tag->setTranslation('name', 'fr', 'mon tag');
$tag->setTranslation('name', 'nl', 'mijn tag');
$tag->save();

//getting translations
$tag->translate('name'); //returns my name
$tag->translate('name', 'fr'); //returns mon tag (optional locale param)

//convenient translations through taggable models
$newsItem->tagsTranslated();// returns tags with slug_translated and name_translated properties
$newsItem->tagsTranslated('fr');// returns tags with slug_translated and name_translated properties set for specified locale

//using tag types
$tag = Tag::findOrCreate('tag 1', 'my type');

//tags have slugs
$tag = Tag::findOrCreate('yet another tag');
$tag->slug; //returns "yet-another-tag"

//tags are sortable
$tag = Tag::findOrCreate('my tag');
$tag->order_column; //returns 1
$tag2 = Tag::findOrCreate('another tag');
$tag2->order_column; //returns 2

//manipulating the order of tags
$tag->swapOrder($anotherTag);

 

For requirements, this package needs Laravel 5.8 or higher, PHP 7.2 or higher and a database that supports json fields such as MySQL 5.7.8 or higher.

Setup

To install this package, run this command :

composer require spatie/laravel-tags

 

Then you can publish the migration with:

php artisan vendor:publish --provider="Spatie\Tags\TagsServiceProvider" --tag="migrations"

 

After the migration has been published you can create the tags and taggables tables by running the migrations:

php artisan migrate

 

Usage

just add the \Spatie\Tags\HasTags trait to your model :

class YourModel extends \Illuminate\Database\Eloquent\Model 
{ use \Spatie\Tags\HasTags;
...
}

 

Then you can add tags:

//adding a single tag 

$yourModel->attachTag('tag 1');

//adding multiple tags

$yourModel->attachTags(['tag 2', 'tag 3']);

//using an instance of \Spatie\Tags\Tag

$yourModel->attach(\Spatie\Tags\Tag::findOrCreate('tag4'));

 

The tags will be stored in the tags-table. When using these functions we’ll make sure that tags are unique and a model will have a tag attached only once.

 

Thanks for reading, you can find more about spatie/laravel-tags in the official website. See you next time.

Tags :