Function description
The easiest way to use it is to provide a powerful "tag" function for your data model.
: Gift: Project address: https://github.com/summerblue/laravel-taggable
This project was modified to the rtconner/laravel-tagging project and added the following functions:
- The tag name is unique;
- Added the etrepat/baum dependency to support unlimited tag nesting;
- Support for automatic generation of Chinese slug pinyin; thanks to super brother for overtrue/pinyin;
- Provides complete test cases to ensure code quality.
Note: this project only supports 5.1 LTS
: Heart: This project is maintained by @ Summer of The EST Group team.
Unlimited label nesting
Integrate etrepat/baum to make tags subordinate.
$ Root = Tag: create (['name' => 'root']); // create a sub-Tag $ child1 = $ Root-> children () -> create (['name' => 'child1 ']); $ child = Tag: create (['name' => 'child2']); $ child-> makeChildOf ($ root); // Create trees in batches $ tagTree = ['name' => 'roottag ', 'Children '=> [['name' => 'l1child1', 'Children '=> [['name' => 'l2child1'], ['name' => 'l2child1 '], ['name' => 'l2child1'],], ['name' => 'l1child2 '], ['name' => 'l1child3 '],]; Tag: buildTree ($ tagTree );
For more Association operations, see etrepat/baum.
Label name rules
- The special symbols and spaces in the tag name are replaced;
- Intelligent tag slug generation generates the Chinese pinyin slug corresponding to the name, for example, tags-> biao-qian. random values are added when the Pinyin is the same;
Label name cleanup: $ normalize_string = EstGroupe \ Taggable \ Util: tagName ($ name ).
Tag: create (['tagname']); // name: tagname/slug: biao-qian-mingTag: create (['table signature']); // name: table signature // slug: biao-qian-ming-3243 (the following 3243 is random, solve pinyin conflicts) Tag: create (['tagname']); // name: tag-name // slug: biao-qian-mingTag: create (['tag! Name ']); // name: tag-name // slug: biao-qian-ming
Installation Instructions: Installation
composer require estgroupe/laravel-taggable "5.1.*"
Install and execute migration
Add the following to the providers array of config/app. php:
'providers' => array( \EstGroupe\Taggable\Providers\TaggingServiceProvider::class,);
php artisan vendor:publish --provider="EstGroupe\Taggable\Providers\TaggingServiceProvider"php artisan migrate
Read the config/tagging. php file carefully.
Create Tag. php
It is not necessary, but we recommend that you create your own project's exclusive Tag. php file.
Modify the config/tagging. php file:
'tag_model'=>'\App\Models\Tag',
Add Taggable Trait "Label status" markTaggable can track whether the model has been tagged:
// `no`$article->is_tagged// `yes`$article->tag('Tag1');$article->is_tagged;// `no`$article->unTag();$article->is_tagged// This is fast$taggedArticles = Article::where('is_tagged', 'yes')->get() First, modify the config/tagging. php file:
'is_tagged_label_enable' => true,
Add the following in the database creation script of your model:
increments('id'); ... // Add this line $table->enum('is_tagged', array('yes', 'no'))->default('no'); ... $table->timestamps(); }); }}"Recommendation tag"To facilitate the "recommendation tag" function, you only need to mark the suggest field as true:
$tag = EstGroupe\Taggable\Model\Tag::where('slug', '=', 'blog')->first();$tag->suggest = true;$tag->save(); You can use the following methods to read data:
$suggestedTags = EstGroupe\Taggable\Model\Tag::suggested()->get();
Override Util class?Most common operations occur in the Util class. to obtain more customization power, create your own Util class and register the service provider:
namespace My\Project\Providers;use EstGroupe\Taggable\Providers\TaggingServiceProvider as ServiceProvider;use EstGroupe\Taggable\Contracts\TaggingUtility;class TaggingServiceProvider extends ServiceProvider { /** * Register the service provider. * * @return void */ public function register() { $this->app->singleton(TaggingUtility::class, function () { return new MyNewUtilClass; }); }} Then
Note that MyNewUtilClass must implement the EstGroupe \ Taggable \ Contracts \ TaggingUtility interface.
Example$ Article = Article: with ('tags')-> first (); // eager load // get all tags foreach ($ article-> tags as $ tag) {echo $ tag-> name. 'with url slug '. $ tag-> slug;} // tag $ article-> tag ('gardening'); // attach the tag $ article-> tag ('gardening, floral '); // attach the tag $ article-> tag (['gardening', 'floral']); // attach the tag $ article-> tag ('gardening ', 'floral'); // attach the tag // tag $ article-> tagWithTagIds ([1, 2, 3]) by using tag ids in batches; // remove the tag $ article-> untag ('cooking'); // remove the Cooking tag $ article-> untag (); // remove all tags // re-tag $ article-> retag (['fruit', 'Fish ']); // delete current tags and save new tags $ article-> retag ('fruit', 'Fish '); $ article-> retag ('Fruit, Fish '); $ tagged = $ article-> tagged; // return Collection of rows tagged to article $ tags = $ article-> tags; // return Collection the actual tags (is slower than using tagged) // gets the bound tag name array $ article-> tagNames (); // get array of related tag names // gets the Article object Article: withAnyTag ('gardening, cooking')-> get (); // fetch articles with any tag listedArticle: withAnyTag (['gardening ', 'cooking'])-> get (); // different syntax, same result as aboveArticle :: withAnyTag ('gardening ', 'cooking')-> get (); // different syntax, same result as above // Obtain the Article object Article with the "full inclusion" label:: withAllTags ('gardening, cooking')-> get (); // only fetch articles with all the tagsArticle: withAllTags (['gardening', 'cooking']) -> get (); Article: withAllTags ('gardening', 'cooking')-> get (); EstGroupe \ Taggable \ Model \ Tag: where ('count ', '>', 2)-> get (); // return all tags used more than twiceArticle: existingTags (); // return collection of all existing tags on articany articles You can use the following tag reading function:
// Use slug to obtain the Tag: byTagSlug ('biao-qian-Ming')-> first (); // Obtain the Tag by name :: byTagName ('tagname')-> first (); // Obtain the Tag array Tag through the name array: byTagNames (['tagname', 'tag2 ', 'tag3'])-> first (); // Obtain the Tag array Tag: byTagIds ([, 3])-> first (); // obtain the ID array through the name array $ ids = Tag: idsByNames (['tagname', 'tag2', 'tag3'])-> all (); // [1, 2, 3]Tag eventTaggabletrait provides the following two events:
EstGroupe\Taggable\Events\TagAdded;EstGroupe\Taggable\Events\TagRemoved;
Listen to tag events:
\Event::listen(EstGroupe\Taggable\Events\TagAdded::class, function($article){ \Log::debug($article->title . ' was tagged');});Unit testFor basic test cases, see tests/CommonUsageTest. php.
Run the test:
composer installvendor/bin/phpunit --verbose
Thanks
- Special Thanks to: Robert Conner-http://smartersoftware.net
- Overtrue/pinyin
- Etrepat/baum
- Made with love by The EST Group-http://estgroupe.com/