robotsinside / laravel-tag-it
A package for tagging Laravel Eloquent models.
This package's canonical repository appears to be gone and the package has been frozen as a result. Email us for help if needed.
Installs: 127
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 1
pkg:composer/robotsinside/laravel-tag-it
Requires
- php: >=7.0.0
- illuminate/database: >=5.0
- illuminate/support: >=5.0
Requires (Dev)
- orchestra/testbench: ^5.1
- phpunit/phpunit: ^9.1
This package is auto-updated.
Last update: 2020-12-03 04:06:57 UTC
README
An Eloquent tagging package for Laravel. This package is a sibling of Laravel Categorise It, which can be used to categorise Eloquent models. The API is pretty much the same as this one.
Installation
- Install using Composer
composer require robotsinside/laravel-tag-it
- Register the service provider in
config/app.php
/* * Package Service Providers... */ \RobotsInside\TagIt\TagItServiceProvider::class,
Auto-discovery is enabled, so this step can be skipped.
- Publish the migrations
php artisan vendor:publish --provider="RobotsInside\TagIt\TagItServiceProvider" --tag="migrations"
- Migrate the database. This will create two new tables;
tagsandtaggables
php artisan migrate
General usage
Use the RobotsInside\TagIt\Taggable trait in your models.
<?php namespace App; use Illuminate\Database\Eloquent\Model; use RobotsInside\TagIt\Taggable; class Post extends Model { use Taggable; }
You are now ready to start tagging. Models can be tagged by passing an integer, array of integers, a model instance or a collection of models.
<?php use App\Post; use Illuminate\Support\Facades\Route; use RobotsInside\TagIt\Models\Tag; Route::get('/', function () { $tag1 = (new Tag())->make('Tag 1'); $tag2 = (new Tag())->make('Tag 2'); $post = new Post(); $post->title = 'My blog'; $post->save(); $post->tag($tag1); // Or $post->tag(['tag-1']); // Or $post->tag([1, 2]); // Or $post->tag(Tag::get()); });
Untagging models is just as simple.
<?php use App\Post; use Illuminate\Support\Facades\Route; use RobotsInside\TagIt\Models\Tag; Route::get('/', function () { $tag1 = Tag::find(1); $post = Post::where('title', 'My blog')->first(); $post->untag($tag1); // Or $post->untag(['tag-1']); // Or $post->untag([1, 2]); // Or $post->untag(Tag::get()); // Or $post->untag(); // remove all tags });
Scopes
Each time a RobotsInside\TagIt\Models\Tag is used, the count column in the tags table is incremented. When a tag is removed, the count is decremented until it is zero.
This packages comes with a number of pre-defined scopes to make queries against the count column easier, namely >=, >, <= and < contstrains, for example:
Tag::usedGte(1);Tag::usedGt(2);Tag::usedLte(3);Tag::usedLt(4);
In addition, a scope on the Taggable model is provided to constrain records created within the given time frame. This scope supports human readable values including days, months and years in both singular and plural formats, for example:
Taggable::taggedWithin('7 days');Taggable::taggedWithin('1 month');Taggable::taggedWithin('2 years');
Credits
License
The MIT License (MIT). Please see License File for more information.