Package Data | |
---|---|
Maintainer Username: | g0110280 |
Package Create Date: | 2015-10-06 |
Package Last Update: | 2018-01-22 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-12-15 15:17:56 |
Package Statistics | |
---|---|
Total Downloads: | 33 |
Monthly Downloads: | 0 |
Daily Downloads: | 0 |
Total Stars: | 3 |
Total Watchers: | 7 |
Total Forks: | 0 |
Total Open Issues: | 0 |
this api can handle tag in independent tables or use only one table
composer.json:
"require" : {
"unisharp/laravel-taggable" : "dev-master"
},
"repositories": {
"type": "git",
"url": "https://github.com/UniSharp/laravel-taggable.git"
}
save it and then
composer update
config/app.php
Unisharp\Taggable\TaggableServiceProvider::class,
IndependentTaggable: it's use independent table to save your tag
eg. if Product is exists, you can use commad to generate ProductTag Model
IndependentCategorizable: it's can categorize for your model
Tag is a many to many relationship, a taggalble entity can tag many tags, and there're many entities can belong a tag. However, a entity can only belong one category, it's one to one relation, and there're many entities can belong a category.
I assume Product model already exists, and you want make this model be taggable. if you don't have any models, you can use following builtin command to generate it
php artisan make:model Product --migration
and use following command to generate tag table and model for your Product
php artisan taggable:independent_tag_table Product
You will see there's ProductTag model under app/
folder
Now, add trait for your product model let it be taggable.
use Unisharp\Taggable\Traits\IndependentTaggable;
class Product extends Model
{
use IndependentTaggable
}
$product = Product::find(1);
$product->tag('new_tag'); // only string
$product->tag('tag1', 'tag2', 'tag3'); // multi string also work
$product->tag(['tag1', 'tag2']); // array is acceptable
$product->untag('new_tag');
$product->untag('tag1', 'tag2', 'tag3');
$product->untag(['tag1', 'tag2']);
$product->tags // it will return ProductTag back
$tag = ProductTag::find(1);
$tag->entities // it will return Products back
Just like independent tag, you can generate independent category migration by command.
php artisan taggable:independent_category_table Product
it will generate ProductCategory for Product
and you can add category trait for Product just like before.
use Unisharp\Taggable\Traits\IndependentIndependentCategorizable;
class Product extends Model
{
use IndependentCategorizable;
}
$product = Product::find(1);
$product->categorize('free'); // use string
$product->categorize(1); // use product_category id
$product->decategorize();
$product->catetory // list its category, it return ProductCategory