# Tags

![](/files/Ffdv3MWD9UR8RdaVylNp)

### Using the Tags field with [Spatie Laravel tags](https://github.com/spatie/laravel-tags)

```php
protected function fields()
{
    return [
        Tags::make('Tags')//The field expects a comma separated array.
            ->default($this->model->exists ? $this->model->tags()->pluck('name') : [])
            ->relation() //save the items with saveFoo() hook, se below
            ->deferEntangle(false);
            //->errorMsg(...)->help(...)->placeholder(...) //default in translation files
            //->rules(...); //applied to each tag, default = 'string|alpha_num|between:3,25'
    ];
}
```

### Save the tags

If your tags are a relationship, you probably want to save the data [manually](/tall-forms/concept/manually-saving-data.md).

```php
protected function saveTags($validated_array)
{
    //example; remove duplicates, ucfirst on each array item
    $cleaned = array_map('ucfirst', array_unique($validated_array));
    //Spatie sync tags
    $this->model->syncTags($cleaned);
}
```

### �Translation files

The default values for some slots are in the [translation](/tall-forms/installation/manual-installation/localisation.md) files

```
->errorMsg(...)->help(...)->placeholder(...)
```

## Headless

Component

```php
public array $tagsDefault = [];
public array $tags = [];

//protected $rules = [ ... ];

public function mount(?User $user)
{
    $this->mount_form($user);
    $this->tagsDefault = $this->model->exists ? $this->model->tags()->pluck('name') : []
}

protected function submit() { ... validate, sync model tags }

public function render() { ... render the view }
```

blade view

```html
<form wire:submit.prevent="submit">

    <x-tall-tags :field="Tags::blade('Tags')->default($tagsDefault)" />
    
    <button type="submit">Submit</button>
    
</form>
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://tina-hammar.gitbook.io/tall-forms/field/fields/tags.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
