laravel 5.3 配置 Taggable 插件,实现打标签功能

1.创建laravel项目,指定版本

composer create-project laravel/laravel test_laravel 5.3.30 --prefer-dist

2.安装插件

composer require estgroupe/laravel-taggable "5.1.*"

3.在config/app.php的服务提供者中,加入

EstGroupe\Taggable\Providers\TaggingServiceProvider::class,

4.导出插件的配置文件

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.php

'tag_model' => '\App\Models\Tag',
'is_tagged_label_enable' => true,

8.由于这个插件是5.1版本的,里面的有些方法已经被弃用,我们需要对其进行修改:vendor/estgroupe/src/Model/ 两个model文件,搜索lists 替换成pluck

lists => pluck

9.在运行时,会出现以下错误:

  Call to a member function normalizeTagName() on null

   经过调试,是Tag.php 这个model 的 $taggingUtility 变量为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表

 

posted @ 2017-03-11 19:24  DoAgain  阅读(593)  评论(0)    收藏  举报