hamedrajabpour / laravel-tags
Add tags and taggable behaviour to your Laravel app
Requires
- php: ^8.0
- laravel/framework: ^8.67|^9.0|^10.0|^11.0
- nesbot/carbon: ^3.8
- spatie/eloquent-sortable: ^3.10|^4.0
- spatie/laravel-package-tools: ^1.4
Requires (Dev)
- orchestra/testbench: ^6.13|^7.0|^8.0|^9.0
- pestphp/pest: ^1.22|^2.0
- phpunit/phpunit: ^9.5.2
README
This package offers taggable behaviour for your models. After the package is installed the only thing you have to do is add the HasTags
trait to an Eloquent model to make it taggable.
The Main Diffrence Between This Package & Spatie that removed translation & slug
Here are some code examples:
// apply HasTags trait to a model use Illuminate\Database\Eloquent\Model; use Spatie\Tags\HasTags; class NewsItem extends Model { use HasTags; // ... }
// create a model with some tags $newsItem = NewsItem::create([ 'name' => 'The Article Title', 'tags' => ['first tag', 'second tag'], //tags will be created if they don't exist ]); // attaching tags $newsItem->attachTag('third tag'); $newsItem->attachTag('third tag','some_type'); $newsItem->attachTags(['fourth tag', 'fifth tag']); $newsItem->attachTags(['fourth_tag','fifth_tag'],'some_type'); // detaching tags $newsItem->detachTag('third tag'); $newsItem->detachTag('third tag','some_type'); $newsItem->detachTags(['fourth tag', 'fifth tag']); $newsItem->detachTags(['fourth tag', 'fifth tag'],'some_type'); // get all tags of a model $newsItem->tags; // syncing tags $newsItem->syncTags(['first tag', 'second tag']); // all other tags on this model will be detached // syncing tags with a type $newsItem->syncTagsWithType(['category 1', 'category 2'], 'categories'); $newsItem->syncTagsWithType(['topic 1', 'topic 2'], 'topics'); // retrieving tags with a type $newsItem->tagsWithType('categories'); $newsItem->tagsWithType('topics'); // retrieving models that have any of the given tags NewsItem::withAnyTags(['first tag', 'second tag'])->get(); // retrieve models that have all of the given tags NewsItem::withAllTags(['first tag', 'second tag'])->get(); // retrieve models that don't have any of the given tags NewsItem::withoutTags(['first tag', 'second tag'])->get(); // using tag types $tag = Tag::findOrCreate('tag 1', 'my type'); // 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);
Requirements
This package requires Laravel 8 or higher, PHP 8 or higher, and a database that supports json
fields and MySQL compatible functions.
Installation
You can install the package via composer:
composer require hamedrajabpour/laravel-tags
The package will automatically register itself.
You can publish the migration with:
php artisan vendor:publish --provider="Spatie\Tags\TagsServiceProvider" --tag="tags-migrations"
After the migration has been published you can create the tags
and taggables
tables by running the migrations:
php artisan migrate
You can optionally publish the config file with:
php artisan vendor:publish --provider="Spatie\Tags\TagsServiceProvider" --tag="tags-config"
Testing
- Copy
phpunit.xml.dist
tophpunit.xml
and fill in your database credentials. - Run
composer test
.
License
The MIT License (MIT). Please see License File for more information.