vendocrat/laravel-tags

This package is abandoned and no longer maintained. The author suggests using the lecturize/laravel-tags package instead.

Simple way to tag Eloquent models in Laravel 5.

v0.1.0 2017-07-28 17:31 UTC

This package is not auto-updated.

Last update: 2019-02-20 18:41:20 UTC


README

Latest Stable Version Total Downloads License

Laravel Tags

Simple way to tag Eloquent models in Laravel 5.

Important Notice

This package is a work in progress, please use with care and feel free to report any issues or ideas you may have!

We've transferred this package to a new owner and therefor updated the namespaces to Lecturize\Tags. The config file is now config/lecturize.php.

Installation

Require the package from your composer.json file

"require": {
    "lecturize/laravel-tags": "dev-master"
}

and run $ composer update or both in one with $ composer require lecturize/laravel-tags.

Next register the service provider and (optional) facade to your config/app.php file

'providers' => [
    // ...
    Cviebrock\EloquentSluggable\ServiceProvider::class,
    Lecturize\Tags\TagsServiceProvider::class
];

Configuration & Migration

$ php artisan vendor:publish --provider="Cviebrock\EloquentSluggable\ServiceProvider"
$ php artisan vendor:publish --provider="Lecturize\Tags\TagsServiceProvider"

This will create a config/sluggable.php, a config/lecturize.php and a migration file, that you'll have to run like so:

$ php artisan migrate

Usage

First, add our HasTags trait to your model.

<?php namespace App\Models;

use Lecturize\Tags\Traits\HasTags;

class Post extends Model
{
    use HasTags;

    // ...
}
?>
Get all tags for a model
$tags = $model->tags();
Check if model is tagged with a given tag
$tags = $model->hasTag('Check');
Add one or more tags
$tags = $model->tag('My First Tag');
$tags = $model->tag(['First', 'Second', 'Third']);
Remove given tags from model
$tags = $model->untag('Remove');
Replace all existing tags for a model with new ones
$tags = $model->retag('A New Tag');
$tags = $model->retag(['Fourth', 'Fifth', 'Sixth']);
Remove all existing tags from model
$tags = $model->detag();
Get a list of all tags
$tags = $model->listTags();

This is a convenience method for $model->tags->pluck('tag')

Scopes

There are two scopes included to fluently query models (e.g. Posts) with given tags.

$posts = Post::withTag('My First Tag')->get();
$posts = Post::withTags(['First', 'Second', 'Third'])->get();

License

Licensed under MIT license.

Author

Handcrafted with love by Alexander Manfred Poellmann in Vienna & Rome.