Giter Club home page Giter Club logo

laravel-tags's Introduction

Add tags and taggable behaviour to a Laravel app

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

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://docs.spatie.be/laravel-tags/v2.

Here are some code examples:

//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']); // all other tags on this model will be detached

//syncing tags with a type
$newsItem->syncTagsWithType(['tag1', 'tag2'], 'typeA'); 
$newsItem->syncTagsWithType(['tag1', 'tag2'], 'typeB'); 

//retrieving tags with a type
$newsItem->tagsWithType('typeA'); 
$newsItem->tagsWithType('typeB'); 

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

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

//translating a tag
$tag = Tag::findOrCreate('my tag');
$tag->setTranslation('fr', 'mon tag');
$tag->setTranslation('nl', 'mijn tag');
$tag->save();

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

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

Requirements

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

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="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="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.
     */
    'slugger' => 'str_slug',
];

Documentation

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

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 [email protected] instead of using the issue tracker.

Testing

  1. Copy .env.example to .env and fill in your database credentials.
  2. Run vendor/bin/phpunit.

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] 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, Samberstraat 69D, 2060 Antwerp, Belgium.

We publish all received postcards on our company website.

Credits

Support us

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

Does your business depend on our contributions? Reach out and support us on Patreon. All pledges will be dedicated to allocating workforce on maintenance and new awesome stuff.

License

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

laravel-tags's People

Contributors

akoepcke avatar alexvanderbist avatar bsn4 avatar ctf0 avatar drbyte avatar fetzi avatar fgilio avatar freekmurze avatar jodiedunlop avatar lasselehtinen avatar mozami avatar omranic avatar pmatseykanets avatar rodrigopedra avatar sebastiandedeyne avatar selimsalihovic avatar tvke avatar wyrihaximus avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.