twom/laravel-taggable

此包最新版本(0.1)没有提供许可证信息。

标签系统

0.1 2020-05-22 05:05 UTC

This package is auto-updated.

Last update: 2024-09-22 14:30:10 UTC


README

安装

composer require twom/laravel-taggable

您必须将服务提供者添加到 config/app.php

'providers' => [
	 // for laravel 5.8 and below
	 \Twom\Taggable\TwomTaggableServiceProvider::class,
];

发布您的配置文件和迁移文件

php artisan vendor:publish

运行迁移

注意:创建可标签化的表。

php artisan migrate

配置

config/taggable.php

return [  
    'model' => \Twom\Taggable\Models\Tag::class,
    'filter_condition' => 'where', // can be 'like', this is default condition
];

让我们开始使用

您的可标签化模型

注意:应该使用来自 Twom\Taggable\Traits\TaggableTaggable 特性

namespace App;  
  
use Illuminate\Database\Eloquent\Model;  
use Twom\Taggable\Traits\Taggable;  
  
class Post extends Model  
{  
  use Taggable;  
  
  public $timestamps = false;  
  
  protected $fillable = [  
	  'title', // and another fields
  ];  
}

可标签化选项的使用

/** @var Post $post */
$post = Post::query()->find(1);

//	just add (attach) tags
$post->tag("sport,gym");

//	sync tags, detach all and attach passed tags
$post->stag("football");

//	delete (detach) tags
$post->detag("football");