1.建立laravel项目,指定版本php
composer create-project laravel/laravel test_laravel 5.3.30 --prefer-dist
2.安装插件laravel
composer require estgroupe/laravel-taggable "5.1.*"
3.在config/app.php的服务提供者中,加入git
EstGroupe\Taggable\Providers\TaggingServiceProvider::class,
4.导出插件的配置文件github
php artisan vendor:publish --provider="EstGroupe\Taggable\Providers\TaggingServiceProvider"
5.配置好数据库信息,运行数据库
php artisan migrate
6.建立本身的Tag.php文件数组
<?php namespace App\Models; use EstGroupe\Taggable\Model\Tag as TaggableTag; class Tag extends TaggableTag { // Model code go here }
7.修改配置文件config/taggable.phpapp
'tag_model' => '\App\Models\Tag',
'is_tagged_label_enable' => true,
8.因为这个插件是5.1版本的,里面的有些方法已经被弃用,咱们须要对其进行修改:vendor/estgroupe/src/Model/ 两个model文件,搜索lists 替换成pluckcomposer
lists => pluck
9.在运行时,会出现如下错误:ide
Call to a member function normalizeTagName() on null
函数
$this->taggingUtility = app(TaggingUtility::class);
在这个类中,有定义$taggingUtility 的地方所有换成 app(TaggingUtility::class)
10.在这个插件依赖的 overtrue/pinyin 插件中,原版本升级以后有个函数进行了修改,在Util.php中,搜索 permlink,对其进行如下替换
permlink=>permalink
11.建立一个测试model
<?php namespace App\Model; use EstGroupe\Taggable\Taggable; use Illuminate\Database\Eloquent\Model; class Article extends Model { use Taggable; protected $table = 'article'; }
12.进行测试
<?php namespace App\Http\Controllers; use App\Model\Article; use \EstGroupe\Taggable\Model\Tag; class Test extends Controller { public function t1() { $article = Article::first(); var_dump($article->is_tagged); $result = $article->tag('abc'); var_dump($result); var_dump($article->is_tagged); } }
附:article 数据表
CREATE TABLE `article` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `is_tagged` enum('yes','no') NOT NULL DEFAULT 'no', `content` varchar(100) DEFAULT NULL, `updated_at` timestamp NULL DEFAULT NULL, `created_at` timestamp NULL DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
参考网址:
https://laravel-china.org/topics/2123
https://github.com/etrepat/baum
https://github.com/etrepat/baum
多级标签经过测试也能够,可是参考网站的数组定义错误了,会出错。
若是修改了配置文件的表名,须要在Taggable.php文件的大概67行位置,改为
return $this->morphToMany(static::$taggingUtility->tagModelString(), 'taggable', config('taggable.taggables_table_name'));
否则,他会去寻找taggables表