Using tags | laravel-tags | Spatie

 SPATIE

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

spatie.be/open-source

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

 Version   v4   v3

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

  Using tags
- [ 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                                                                                                                                                                                                                                    `

Using tags
==========

###  On this page

1. [ Attaching tags ](#content-attaching-tags)
2. [ Detaching tags ](#content-detaching-tags)
3. [ Syncing tags ](#content-syncing-tags)
4. [ Managing tags ](#content-managing-tags)
5. [ Finding tags ](#content-finding-tags)
6. [ Getting types ](#content-getting-types)

To make an Eloquent model taggable just add the `\Spatie\Tags\HasTags` trait to it:

```
class YourModel extends \Illuminate\Database\Eloquent\Model
{
    use \Spatie\Tags\HasTags;

    ...
}
```

Attaching tags
--------------------------------------------------------------------------------------------------

Here's how you can add some tags:

```
//adding a single tag
$yourModel->attachTag('tag 1');

//adding multiple tags
$yourModel->attachTags(['tag 2', 'tag 3']);

//using an instance of \Spatie\Tags\Tag
$yourModel->attach(\Spatie\Tags\Tag::findOrCreate('tag4'));
```

The tags will be stored in the `tags`-table. When using these functions we'll make sure that tags are unique and a model will have a tag attached only once.

Detaching tags
--------------------------------------------------------------------------------------------------

Here's how tags can be detached:

```
//using a string
$yourModel->detachTag('tag 1');

//using an array
$yourModel->detachTags(['tag 2', 'tag 3']);

//using an instance of \Spatie\Tags\Tag
$yourModel->detach(\Spatie\Tags\Tag::Find('tag4'));
```

Syncing tags
--------------------------------------------------------------------------------------------

By syncing tags the package will make sure only the tags given will be attached to the models. All other tags that were on the model will be detached.

```
$yourModel->syncTags(['tag 2', 'tag 3']);
```

Managing tags
-----------------------------------------------------------------------------------------------

Tags are stored in the `tags` table and can be managed with the included `Spatie\Tags\Tag`-model.

```
//create a tag
$tag = Tag::create(['name' => 'my tag']);

//update a tag
$tag->name = 'another tag';
$tag->save();

//use "findFromString" instead of "find" to retrieve a certain tag
$tag = Tag::findFromString('another tag')

//create a tag if it doesn't exist yet
$tag = Tag::findOrCreateFromString('yet another tag');

//delete a tag
$tag->delete();
```

Finding tags
--------------------------------------------------------------------------------------------

You can find all tags containing a specific value with the `containing` scope.

```
Tag::findOrCreate('one');
Tag::findOrCreate('another-one');
Tag::findOrCreate('another-ONE-with-different-casing');
Tag::findOrCreate('two');

Tag::containing('on')->get(); // will return all tags except `two`
```

Getting types
-----------------------------------------------------------------------------------------------

You can fetch a collection of all registered tag types by using the static method `getTypes()`:

```
Tag::getTypes();
```

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
