GithubHelp home page GithubHelp logo

taggy's Introduction

Installation

Require this package with composer. It is recommended to only require the package for development.

composer require putheng/taggy

Laravel 5.5 uses Package Auto-Discovery, so doesn't require you to manually add the ServiceProvider.

Setting up from scratch

Laravel 5.5+:

If you don't use auto-discovery, add the ServiceProvider to the providers array in config/app.php

Putheng\Taggy\TaggyServiceProvider::class,

The schema

For Laravel 5 migration

php artisan migrate

The model

Your model should use Putheng\Taggy\TaggableTrait trait to enable tags:

use Putheng\Taggy\TaggableTrait;

class Lession extends Model {
    use TaggableTrait;
}

Usage

Seed

Seed the tags table

use Putheng\Taggy\Models\Tag;

$tags = [
	[
		'name' => 'PHP',
		'slug' => str_slug('PHP')
	],
	[
		'name' => 'Laravel',
		'slug' => str_slug('Laravel')
	],
	[
		'name' => 'Testing',
		'slug' => str_slug('Testing')
	],
	[
		'name' => 'Redis',
		'slug' => str_slug('Redis')
	],
];

Tag::insert($tags);

Tag a lession

Create a new lession and tags

use App\Lession;

$lession = new Lession;
$lession->title = 'a new lession';
$lession->save();

# name or slug version of value in tags table
$lession->tag(['Laravel', 'php']);

# tag from a collections of model
$tags = Putheng\Taggy\Models\Tag::whereIn('slug', ['php', 'laravel'])->get();
$lession->tag($tags);

# tag from a model
$tag = Putheng\Taggy\Models\Tag::where('name', 'Laravel')->first();
$lession->tag($tag);

Tag to an existing lessions

$lession = Lession::find(1);
$lession->tag(['Redis']);

Untag from a lession

$lession = Lession::find(1);
$lession->untag(['Redis']);

Retag from a lession, will remove all tags from lession and retag again

$lession = Lession::find(1);
$lession->retag(['Redis']);

Show tags of a lession

$lession = Lession::find(1);

# return collection of tags
$tags = $lession->tags;

Get lessions of any tags

# get lessions of any tags from array of tags's slug
$lessions = Lession::withAnyTag(['php', 'laravel']);

# return collection of lession
$lessions->get();

Get lessions of all tags

# get lessions of all tags from array of tags's slug
$lessions = Lession::withAllTags(['php', 'laravel']);

# return collection of lession
$lessions->get();

Get lessions has tags

# get lessions that has tags from array of tags's slug
$lessions = Lession::hasTags(['php', 'laravel']);

# return collection of lession
$lessions->get();

A scope to retrieve any tags where the count

use Putheng\Taggy\Models\Tag;

# is greater than or equal to the given value.
$tags = Tag::useGte();

# is greater than to the given value.
$tags = Tag::useGt();

# is less than or equal to the given value.
$tags = Tag::useLte();

# is less than the given value.
$tags = Tag::useLt();

taggy's People

Contributors

putheng avatar

Watchers

 avatar

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.