aammui/laravel-taggable

Assign Category and Tags to a model

3.0.0 2023-02-16 16:32 UTC

This package is auto-updated.

Last update: 2024-11-03 20:47:48 UTC


README

Introduction

Category and Tags are often useful while working working with Laravel Model. This solution enable categories and tags for any Models in Laravel. Here step by step setup is given below.

Content

  1. Installtion
  2. Publish Assets and Migration
  3. Use Trait in model
  4. Call from anywhere
  5. Display in blade

Step1: Installation

composer require aammui/laravel-taggable

Step2: Publish Migrations assets

php artisan vendor:publish --provider="Aammui\LaravelTaggable\LaravelTaggableServiceProvider"
php artisan migrate

Step3: Use Traits

Add HasCatgory and HasTag Traits in your model.

<?php 

namespace App;

use Aammui\LaravelTaggable\Traits\HasCategory;
use Aammui\LaravelTaggable\Traits\HasTag;

class Post extends Model {
    use HasCategory, HasTag;
}

Step4: Call from anywhere.

<?php
$post = Post::create(['name'=>'Post']);
$post->addTag('Tags');
$post->addTag(['Tags','Category']);
$post->addCategory('Category');
$post->addCategory(['Category', 'Laravel Category']);

Step5: Display In Blade

<ul>
@foreach($posts->category as $item)
<li>{{$item->name}}</li>
@endforeach
</ul>