AhmedMub / laravel-tags-with-livewire

How to use spatie/Laravel-tags with Livewire

Home Page:https://freek.dev/609-an-opinionated-tagging-package-for-laravel-apps

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Use spatie/laravel-tags with Livewire

For more information on the package, please refer to the Spatie documentation, as below explanation for those already familiar with it.

After making an Eloquent model taggable by adding the \Spatie\Tags\HasTags trait to it::

// apply HasTags trait to a model
use Illuminate\Database\Eloquent\Model;
use Spatie\Tags\HasTags;

class NewsItem extends Model
{
    use HasTags;
    
    // ...
}
// In your livewire component, you should use model binding and set the wire model:
<input wire:model='name.en' type="text" />

//And for translation in case, you have another input for multi-language functionality
<input wire:model='name.ar' type="text" />

// to use livewire validation
public $name;
protected $rules = [
        'name.en' => ['required', 'string'],
        'name.ar' => ['required', 'string'],
    ];

protected $messages = [
    'name.en.required' => 'This field is required',
    'name.ar.required' => 'This field is required',
];

// here to save the tag with translation if you have any
$tag = new Tag;
$tag->setTranslation('name', 'en', $this->name['en'])
    ->setTranslation('name', 'ar', $this->name['ar'])
    ->save();

// this is to update the tag, already explained in the documentation
$tag = Tag::findOrFail($id);
$tag->setTranslation('name', 'en', $this->name['en']);
$tag->setTranslation('name', 'ar', $this->name['ar']);
$tag->save();

// to display all the tags with any selected translation that exists
@foreach ($tags as $tag)
    <div> {{$tag->getTranslation('name', 'en')}} </div>
    <div> {{$tag->getTranslation('name', 'ar')}} </div>
@endforeach

About

How to use spatie/Laravel-tags with Livewire

https://freek.dev/609-an-opinionated-tagging-package-for-laravel-apps

License:MIT License


Languages

Language:PHP 100.0%