Code Monkey home page Code Monkey logo

laravel-tags's Introduction

Latest Stable Version Total Downloads License

Laravel Tags

Simple way to tag eloquent models in Laravel.

Installation

Require the package from your composer.json file

"require": {
    "lecturize/laravel-tags": "^1.0"
}

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

Configuration & Migration

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

This will publish a config/sluggable.php, a config/lecturize.php and some migration files, that you'll have to run:

$ php artisan migrate

For migrations to be properly published ensure that you have added the directory database/migrations to the classmap in your projects composer.json.

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.