1. Go to this page and download the library: Download estgroupe/laravel-taggable library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
namespace App\Models;
use EstGroupe\Taggable\Model\Tag as TaggableTag;
class Tag extends TaggableTag
{
// Model code go here
}
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use EstGroupe\Taggable\Taggable;
class Article extends \Illuminate\Database\Eloquent\Model {
use Taggable;
}
// `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()
'is_tagged_label_enable' => true,
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateArticlesTable extends Migration {
public function up()
{
Schema::create('weibo_statuses', function(Blueprint $table) {
$table->increments('id');
...
// Add this line
$table->enum('is_tagged', array('yes', 'no'))->default('no');
...
$table->timestamps();
});
}
}
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;
});
}
}
$article = Article::with('tags')->first(); // eager load
// Get all the article tagged tags
foreach($article->tags as $tag) {
echo $tag->name . ' with url slug of ' . $tag->slug;
}
// Tag some tag/tags
$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
// Using tag_id batch tag
$article->tagWithTagIds([1,2,3]);
// Remove tags
$article->untag('Cooking'); // remove Cooking tag
$article->untag(); // remove all tags
// Retag
$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)
// Get array of related tag names
$article->tagNames();
// Fetch articles with any tag listed
Article::withAnyTag('Gardening, Cooking')->get();
Article::withAnyTag(['Gardening','Cooking'])->get(); // different syntax, same result as above
Article::withAnyTag('Gardening','Cooking')->get(); // different syntax, same result as above
// Only fetch articles with all the tags
Article::withAllTags('Gardening, Cooking')->get();
Article::withAllTags(['Gardening', 'Cooking'])->get();
Article::withAllTags('Gardening', 'Cooking')->get();
// Return all tags used more than twice
EstGroupe\Taggable\Model\Tag::where('count', '>', 2)->get();
// Return collection of all existing tags on any articles
Article::existingTags();
// By tag slug
Tag::byTagSlug('biao-qian-ming')->first();
// By tag name
Tag::byTagName('tag1')->first();
// Using names
Tag::byTagNames(['tag1', 'tag12', 'tag13'])->first();
// Using Tag ids array
Tag::byTagIds([1,2,3])->first();
// Using name to get tag ids array
$ids = Tag::idsByNames(['标签名', '标签2', '标签3'])->all();
// [1,2,3]