Skip to content

AlexVanderbist/laravel-tags

Repository files navigation

Add tags and taggable behaviour to a Laravel app

Latest Version on Packagist Software License Build Status SensioLabsInsight Quality Score StyleCI Total Downloads

This package provides you with a HasTags trait with which you can easily add tags to your Eloquent models. Once the package is set up you can do stuff like this:

// 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');

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

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

This is the core functionality of almost every other tag package out there. What makes this spatie/laravel-tags unique is the built in support for translations, tag types, slugs, and sortable tags.

//attaching a tag with a type
NewsItem::attachTag(Tag::findOrCreate('string', 'myType'));

// the tag model has a scope to retrieve all tags with a certain type
Tag::type('myType')->get()

// tags can hold translations
$tag = Tag::findOrCreate('my tag'); //uses the app's locale
$tag->setTranslation('fr', 'mon tag');
$tag->setTranslation('nl', 'mijn tag');
$tag->save();

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

// tags have slugs 

Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.

Postcardware

You're free to use this package (it's MIT-licensed), but if it makes it to your production environment you are required to send us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Spatie, Samberstraat 69D, 2060 Antwerp, Belgium.

The best postcards will get published on the open source page on our website.

Requirements

This package requires Laravel 5.3 or higher, PHP 7.0 or higher and a database that supports json fields such as MySQL 5.7 or higher.

Installation

You can install the package via composer:

composer require spatie/laravel-tags

Next up, the service provider must be registered:

// config/app.php
'providers' => [
    ...
    Spatie\Tags\TagsServiceProvider::class,

];

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

Documentation

You'll find the documentation on https://docs.spatie.be/laravel-medialibrary/v1.

Find yourself stuck using the package? Found a bug? Do you have general questions or suggestions for improving the media library? 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 freek@spatie.be instead of using the issue tracker.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email freek@spatie.be instead of using the issue tracker.

Credits

About Spatie

Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.

License

The MIT License (MIT). Please see License File for more information.

About

Add tags and taggable behaviour to your Laravel app

Resources

License

Contributing

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%