Introduction | laravel-tags | Spatie

 SPATIE

  Laravel Tags
===============

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-tags](https://spatie.be/docs/laravel-tags/v3)  Introduction

 Version   v4   v3

 Other versions for crawler [v4](https://spatie.be/docs/laravel-tags/v4) [v3](https://spatie.be/docs/laravel-tags/v3)

- [ Introduction ](https://spatie.be/docs/laravel-tags/v3/introduction)
- [ Postcardware ](https://spatie.be/docs/laravel-tags/v3/postcardware)
- [ Requirements ](https://spatie.be/docs/laravel-tags/v3/requirements)
- [ Installation and Setup ](https://spatie.be/docs/laravel-tags/v3/installation-and-setup)
- [ Questions and issues ](https://spatie.be/docs/laravel-tags/v3/questions-and-issues)
- [ Changelog ](https://spatie.be/docs/laravel-tags/v3/changelog)
- [ About us ](https://spatie.be/docs/laravel-tags/v3/about-us)

Basic usage
-----------

- [ Using tags ](https://spatie.be/docs/laravel-tags/v3/basic-usage/using-tags)
- [ Retrieving tagged models ](https://spatie.be/docs/laravel-tags/v3/basic-usage/retrieving-tagged-models)

Advanced usage
--------------

- [ Adding translations ](https://spatie.be/docs/laravel-tags/v3/advanced-usage/adding-translations)
- [ Using types ](https://spatie.be/docs/laravel-tags/v3/advanced-usage/using-types)
- [ Sorting tags ](https://spatie.be/docs/laravel-tags/v3/advanced-usage/sorting-tags)
- [ Using your own tag model ](https://spatie.be/docs/laravel-tags/v3/advanced-usage/using-your-own-tag-model)

      You are viewing the documentation for **an older version** of this package. You can check the version you are using with the following command:

 `                                    composer show spatie/laravel-tags                                                                                                                                                                                                                                    `

 Laravel Tags
==============

 Add tags and taggable behaviour to a Laravel app.

 [    Repository ](https://github.com/spatie/laravel-tags)

    10,365,178

    1,730

Introduction
------------

###  On this page

1. [ We have badges! ](#content-we-have-badges)

This package offers taggable behaviour for your models. After the package is installed the only thing you have to do is to 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](/laravel-tags/v2/advanced-usage/adding-translations), [multiple tag types](/laravel-tags/v2/advanced-usage/using-types) and [sorting capabilities](/laravel-tags/v2/advanced-usage/sorting-tags).

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->detachTag('tag3');
$newsItem->detachTags(['tag4', 'tag5']);

// syncing tags
$newsItem->syncTags(['tag1', 'tag2']); // all other tags on this model will be detached

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

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

// get all tags containing a given value
Tag::containing('test'); // returns all tags that contain 'test'
```

We have badges!
---------------------------------------------------------------------------------------------------

 [![Latest Version on Packagist](https://img.shields.io/packagist/v/spatie/laravel-tags.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-tags) [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](https://github.com/spatie/laravel-tags/blob/master/LICENSE.md) [![Build Status](https://img.shields.io/travis/spatie/laravel-tags/master.svg?style=flat-square)](https://travis-ci.org/spatie/laravel-tags) [![Quality Score](https://img.shields.io/scrutinizer/g/spatie/laravel-tags.svg?style=flat-square)](https://scrutinizer-ci.com/g/spatie/laravel-tags) [![Total Downloads](https://img.shields.io/packagist/dt/spatie/laravel-tags.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-tags)
