hkp22/laravel-taggable

Laravel model tagging package.

1.0 2018-07-04 12:01 UTC

This package is auto-updated.

Last update: 2024-04-21 19:44:42 UTC


README

Laravel Eloquent model tagging package.

Installation

You can install the package via composer:

composer require hkp22/laravel-taggable

Usage

Prepare Taggable model

use Hkp22\LaravelTaggable\Traits\Taggable;
use Illuminate\Database\Eloquent\Model as Eloquent;

class Lesson extends Eloquent
{
    use Taggable;
}

Registering package

Include the service provider within app/config/app.php:

'providers' => [
    Hkp22\LaravelTaggable\TaggableServiceProvider::class,
],

Tag Eloquent model

Eg: Tagging a lesson Model.

$lesson->tag(['laravel', 'php']);

or

$tags = $tags = Tag::whereIn('slug', ['laravel', 'testing'])->get();

$lesson->tag($tags);

Un-tag eloquent model

Eg: Un-tagging lesson model.

// Un-tag single tag.
$lesson->untag(['laravel']);

// Un-tag all tags.
$lesson->untag();    

Re-tag eloquent model

// Tag
$lesson->tag(['laravel', 'testing']);

// Re-tag
$lesson->retag(['laravel', 'postgres', 'redis']);

Total tagged entities count.

$tag = Tag::first();

$tag->count;

Get model with any given tag

$lessons = Lesson::withAnyTag(['php'])->get();

Get model with only given tag

$lessons = Lesson::withAllTags(['php', 'laravel', 'testing'])->get();