spatie / laravel-tags
Add tags and taggable behaviour to your Laravel app
Fund package maintenance!
spatie
Installs: 6 230 025
Dependents: 78
Suggesters: 0
Security: 0
Stars: 1 622
Watchers: 17
Forks: 281
Open Issues: 2
Requires
- php: ^8.0
- laravel/framework: ^8.67|^9.0|^10.0|^11.0
- nesbot/carbon: ^2.63|^3.0
- spatie/eloquent-sortable: ^3.10|^4.0
- spatie/laravel-package-tools: ^1.4
- spatie/laravel-translatable: ^4.6|^5.0|^6.0
Requires (Dev)
- orchestra/testbench: ^6.13|^7.0|^8.0|^9.0
- pestphp/pest: ^1.22|^2.0
- phpunit/phpunit: ^9.5.2
- dev-main
- 4.7.0
- 4.6.1
- 4.6.0
- 4.5.2
- 4.5.1
- 4.5.0
- 4.4.0
- 4.3.7
- 4.3.6
- 4.3.5
- 4.3.4
- 4.3.3
- 4.3.2
- 4.3.1
- 4.3.0
- 4.2.1
- 4.2.0
- 4.1.0
- 4.0.5
- 4.0.4
- 4.0.3
- 4.0.2
- 4.0.1
- 4.0.0
- v3.x-dev
- 3.1.0
- 3.0.2
- 3.0.1
- 3.0.0
- 2.7.2
- 2.7.1
- 2.7.0
- 2.6.2
- 2.6.1
- 2.6.0
- 2.5.4
- 2.5.3
- 2.5.2
- 2.5.1
- 2.5.0
- 2.4.5
- 2.4.4
- 2.4.3
- 2.4.2
- 2.4.1
- 2.3.0
- 2.2.2
- 2.2.1
- 2.2.0
- 2.1.6
- 2.1.5
- 2.1.4
- 2.1.3
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.2
- 2.0.1
- 2.0.0
- 1.4.1
- 1.4.0
- 1.3.5
- 1.3.4
- 1.3.3
- 1.3.2
- 1.3.1
- 1.3.0
- 1.2.0
- 1.1.1
- 1.1.0
- 1.0.2
- 1.0.1
- 1.0.0
- 0.0.1
This package is auto-updated.
Last update: 2024-11-03 16:04:13 UTC
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.
But we didn't stop with the regular tagging capabilities you find in every package. Laravel Tags comes with batteries included. Out of the box it has support for translating tags, multiple tag types and sorting capabilities.
You'll find the documentation on https://spatie.be/docs/laravel-tags.
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(); // 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); // checking if a model has a tag $newsItem->hasTag('first tag'); $newsItem->hasTag('first tag', 'some_type');
Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.
Support us
We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
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 spatie/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"
This is the contents of the published config file:
return [ /* * The given function generates a URL friendly "slug" from the tag name property before saving it. * Defaults to Str::slug (https://laravel.com/docs/5.8/helpers#method-str-slug) */ 'slugger' => null, ];
Documentation
You'll find the documentation on https://docs.spatie.be/laravel-tags/v4.
Find yourself stuck using the package? Found a bug? Do you have general questions or suggestions for improving the laravel-tags
package? Feel free to create an issue on GitHub, we'll try to address it as soon as possible.
If you've found a bug regarding security please mail security@spatie.be instead of using the issue tracker.
Testing
- Copy
phpunit.xml.dist
tophpunit.xml
and fill in your database credentials. - Run
composer test
.
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you've found a bug regarding security please mail security@spatie.be instead of using the issue tracker.
Postcardware
You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.
Our address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium.
We publish all received postcards on our company website.
Credits
License
The MIT License (MIT). Please see License File for more information.