Simple way to tag eloquent models in Laravel.
Require the package from your composer.json
file
"require": {
"lecturize/laravel-tags": "^1.0"
}
and run $ composer update
or both in one with $ composer require lecturize/laravel-tags
.
$ php artisan vendor:publish --provider="Cviebrock\EloquentSluggable\ServiceProvider"
$ php artisan vendor:publish --provider="Lecturize\Tags\TagsServiceProvider"
This will publish a config/sluggable.php
, a config/lecturize.php
and some migration files, that you'll have to run:
$ php artisan migrate
For migrations to be properly published ensure that you have added the directory database/migrations
to the classmap in your projects composer.json
.
First, add our HasTags
trait to your model.
<?php
namespace App\Models;
use Lecturize\Tags\Traits\HasTags;
class Post extends Model
{
use HasTags;
// ...
}
?>
$tags = $model->tags();
$tags = $model->hasTag('Check');
$tags = $model->tag('My First Tag');
$tags = $model->tag(['First', 'Second', 'Third']);
$tags = $model->untag('Remove');
$tags = $model->retag('A New Tag');
$tags = $model->retag(['Fourth', 'Fifth', 'Sixth']);
$tags = $model->detag();
$tags = $model->listTags();
This is a convenience method for $model->tags->pluck('tag')
There are two scopes included to fluently query models (e.g. Posts) with given tags.
$posts = Post::withTag('My First Tag')->get();
$posts = Post::withTags(['First', 'Second', 'Third'])->get();
Licensed under MIT license.
Handcrafted with love by Alexander Manfred Poellmann in Vienna & Rome.