Skip to content
tanthammar edited this page Sep 4, 2020 · 1 revision

Tags field for Spatie Laravel Tags with auto search

image

  • Requires Spatie Laravel Tags
  • support for tag types
  • auto search

tags()

Saving/Syncing the tags in update forms

If the form uses action = create, the package takes care of syncing the tags, you do not have to do anything.

Saving/Syncing the tags in create forms

If the form uses action = create the model does not exist before the user clicks the submit button. The form component has a syncTags() method that you must add to the forms create() method. If your form has multiple tag fields you must call the syncTags() method for each field.

syncTags($field, $tagType)

Example for an Article model with two tag fields

    public function create($form_data) //the create() method is only needed for action="create" forms
    {
        $this->model = User::create($form_data); //the model must exist before the tags can be synced

        //You only need add the syncTags() method if this is an action="create" form.
        //If this is an action="update form", all is synced automatically.
        $this->syncTags('categories', 'article-category'); // this form has two tags fields
        $this->syncTags('tags', 'article-tag'); // you must call the syncTags for each field.
    }

Declaration WITHOUT tag type

Field::make('Tags')
    ->tags()
    ->help('Press ENTER/return to create a new tag, or SELECT an existing tag by CLICKING on it.'),

Declaration WITH tag type

Field::make('Categories')
    ->tags('article-category')
    ->help('Press ENTER/return to create a new tag, or SELECT an existing tag by CLICKING on it.'),

Declaration WITH tag type and tag type SUFFIX

Spatie Laravel Tags uses Spatie Laravel Translatable, for me it is a pain to swap app()->setLocale() in forms to allow the user to save tags in different languages. My way arround it is to use a suffix for the language that I later use when I query the tags. This is of course nothing you have to do, I am just telling why there is a suffix option that you can ignore, or use for something else.

Important to note is that the suffix option in this example does not save the tags in any particular language.

Field::make('Swedish Categories', 'tags-sv')
    ->tags('categories', 'sv') //this will be saved with tag type "categories-sv"
    ->help('Press ENTER/return to create a new tag, or SELECT an existing tag by CLICKING on it.'),

Field::make('English Categories', 'tags-en')
    ->tags('categories', 'en') //this will be saved with tag type "categories-en"
    ->help('Press ENTER/return to create a new tag, or SELECT an existing tag by CLICKING on it.'),

Translated tags

Spatie Laravel Tags uses Spatie Laravel Translatable. If you want to let the user create tags in a specific language you have to set the locale before each field is saved. It is not yet covered with this package. I handle it with a suffix as described above.

SimpleTags field

  • will be released this week, simple input with tag styling for comma separated string value,