TALL-forms
  • TALL-forms
  • Installation
    • Quickstart: Artisan cmd
    • Manual installation
      • Package
      • Translations
      • Css & Theme
      • Javascript
      • Tailwind
      • Laravel Mix
      • Wrapper view
      • Laravel Jetstream/Breeze
  • Upgrade v7 to v8
    • Configuration changes
    • Component & Field changes
    • Blade view changes
    • Notification changes
    • Styling changes
  • Concept
    • Configuration
    • Validation
    • Styling
    • Icons
    • Javascript
    • Layout vs Wrapper
    • Model binding
    • Manually saving data
    • Extend Blade Components
  • Create Forms
    • Form Data
    • Form Types
      • Standard form
      • As modal
      • Without buttons
      • Fields only
      • Headless
    • Form attributes
    • Form buttons
    • Form slots
    • Form methods
    • Lifecycle hooks
    • Render a form
    • Mass generate
  • Fields
    • Declaration
    • Labels
    • Field Slots
    • Field methods
    • Custom field attributes
    • Conditional field
    • Custom view
    • Custom field
    • Custom Livewire component
    • Relations
    • Fields
      • Checkbox
      • Checkboxes
      • FileUpload
      • Honeypot
      • ImageCropper
      • Input
      • Password
      • InputArray
      • KeyVal (array)
      • Radio
      • Range
      • Repeater (array)
      • Search
      • Select & Multiselect
      • Tags
      • TagsSearch
      • Textarea
      • Trix
      • SpatieTags
    • Sponsor Fields
      • More Inputs
      • CKEditor
      • DatePicker
      • Heading
      • Markdown
      • Panels
      • SearchList
      • SelectOptGroup
      • Tabs
      • Trix, file-uploads
  • Blade components
    • Notifications
    • Label wrapper
    • Button
    • Modal blade component
    • Modal form blade component
  • Examples
    • Input examples
    • Array fields example
Powered by GitBook
On this page
  • Using the Tags field with Spatie Laravel tags
  • Save the tags
  • Translation files
  • Headless

Was this helpful?

  1. Fields
  2. Fields

Tags

PreviousSelect & MultiselectNextTagsSearch

Last updated 3 years ago

Was this helpful?

Using the Tags field with

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

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

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

Headless

Component

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

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

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

If your tags are a relationship, you probably want to save the data .

The default values for some slots are in the files

manually
translation
Spatie Laravel tags