成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專(zhuān)欄INFORMATION COLUMN

Laravel Taggable 為你的模型添加打標(biāo)簽功能

lucas / 576人閱讀

摘要:標(biāo)簽名稱(chēng)規(guī)則說(shuō)明標(biāo)簽名里的特殊符號(hào)和空格會(huì)被替代智能標(biāo)簽生成,會(huì)生成對(duì)應(yīng)的中文拼音,如標(biāo)簽,拼音一樣的時(shí)候會(huì)被加上隨機(jī)值標(biāo)簽名清理使用。

本文經(jīng)授權(quán)轉(zhuǎn)自 PHPHub 社區(qū)

功能說(shuō)明

使用最簡(jiǎn)便的方式,為你的數(shù)據(jù)模型提供強(qiáng)大「打標(biāo)簽」功能。

項(xiàng)目地址:https://github.com/summerblue/laravel-taggable

本項(xiàng)目修改于 rtconner/laravel-tagging 項(xiàng)目,增加了一下功能:

標(biāo)簽名唯一;

增加 etrepat/baum 依賴(lài),讓標(biāo)簽支持無(wú)限級(jí)別標(biāo)簽嵌套;

中文 slug 拼音自動(dòng)生成支持,感謝超哥的 overtrue/pinyin;

提供完整的測(cè)試用例,保證代碼質(zhì)量。

注意: 本項(xiàng)目只支持 5.1 LTS

此項(xiàng)目由 The EST Group 團(tuán)隊(duì)的 @Summer 維護(hù)。

無(wú)限級(jí)別標(biāo)簽嵌套

集成 etrepat/baum 讓標(biāo)簽具備從屬關(guān)系。

$root = Tag::create(["name" => "Root"]);

// 創(chuàng)建子標(biāo)簽
$child1 = $root->children()->create(["name" => "Child1"]);

$child = Tag::create(["name" => "Child2"]);
$child->makeChildOf($root);

// 批量構(gòu)建樹(shù)
$tagTree = [
    "name" => "RootTag",
    "children" => [
        ["name" => "L1Child1",
            "children" => [
                ["name" => "L2Child1"],
                ["name" => "L2Child1"],
                ["name" => "L2Child1"],
            ]
        ],
        ["name" => "L1Child2"],
        ["name" => "L1Child3"],
    ]
];

Tag::buildTree($tagTree);

更多關(guān)聯(lián)操作請(qǐng)查看:etrepat/baum 。

標(biāo)簽名稱(chēng)規(guī)則說(shuō)明

標(biāo)簽名里的特殊符號(hào)和空格會(huì)被 - 替代;

智能標(biāo)簽 slug 生成,會(huì)生成 name 對(duì)應(yīng)的中文拼音 slug ,如:標(biāo)簽 -> biao-qian,拼音一樣的時(shí)候會(huì)被加上隨機(jī)值;

標(biāo)簽名清理使用:$normalize_string = EstGroupeTaggableUtil::tagName($name)。

Tag::create(["標(biāo)簽名"]);
// name: 標(biāo)簽名
// slug: biao-qian-ming

Tag::create(["表簽名"]);
// name: 表簽名
// slug: biao-qian-ming-3243 (后面 3243 為隨機(jī),解決拼音沖突)

Tag::create(["標(biāo)簽 名"]);
// name: 標(biāo)簽-名
// slug: biao-qian-ming

Tag::create(["標(biāo)簽!名"]);
// name: 標(biāo)簽-名
// slug: biao-qian-ming
安裝說(shuō)明: 安裝
composer require estgroupe/laravel-taggable "5.1.*"
安裝和執(zhí)行遷移

config/app.phpproviders 數(shù)組中加入:

"providers" => array(
    EstGroupeTaggableProvidersTaggingServiceProvider::class,
);
php artisan vendor:publish --provider="EstGroupeTaggableProvidersTaggingServiceProvider"
php artisan migrate

請(qǐng)仔細(xì)閱讀 config/tagging.php 文件。

創(chuàng)建 Tag.php

不是必須的,不過(guò)建議你創(chuàng)建自己項(xiàng)目專(zhuān)屬的 Tag.php 文件。


修改 config/tagging.php 文件中:

    "tag_model"=>"AppModelsTag",
加入 Taggable Trait

「標(biāo)簽狀態(tài)」標(biāo)示

Taggable 能跟蹤模型是否打過(guò)標(biāo)簽的狀態(tài):

// `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()

首先你需要修改 config/tagging.php 文件中:

"is_tagged_label_enable" => true,

然后在你的模型的數(shù)據(jù)庫(kù)創(chuàng)建腳本里加上:

increments("id");
            ...
            // Add this line
            $table->enum("is_tagged", array("yes", "no"))->default("no");
            ...
            $table->timestamps();
        });
    }
}
「推薦標(biāo)簽」標(biāo)示

方便你實(shí)現(xiàn)「推薦標(biāo)簽」功能,只需要把 suggest 字段標(biāo)示為 true

$tag = EstGroupeTaggableModelTag::where("slug", "=", "blog")->first();
$tag->suggest = true;
$tag->save();

即可以用以下方法讀?。?/p>

$suggestedTags = EstGroupeTaggableModelTag::suggested()->get();
重寫(xiě) Util 類(lèi)?

大部分的通用操作都發(fā)生在 Util 類(lèi),你想獲取更多的定制權(quán)力,請(qǐng)創(chuàng)建自己的 Util 類(lèi),并注冊(cè)服務(wù)提供者:

namespace MyProjectProviders;

use EstGroupeTaggableProvidersTaggingServiceProvider as ServiceProvider;
use EstGroupeTaggableContractsTaggingUtility;

class TaggingServiceProvider extends ServiceProvider {

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        $this->app->singleton(TaggingUtility::class, function () {
            return new MyNewUtilClass;
        });
    }

}

然后在

注意 MyNewUtilClass 必須實(shí)現(xiàn) EstGroupeTaggableContractsTaggingUtility 接口。

使用范例
$article = Article::with("tags")->first(); // eager load

// 獲取所有標(biāo)簽
foreach($article->tags as $tag) {
    echo $tag->name . " with url slug of " . $tag->slug;
}

// 打標(biāo)簽
$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

// 批量通過(guò) tag ids 打標(biāo)簽
$article->tagWithTagIds([1,2,3]);

// 去掉標(biāo)簽
$article->untag("Cooking"); // remove Cooking tag
$article->untag(); // remove all tags

// 重打標(biāo)簽
$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)

// 獲取綁定的標(biāo)簽名稱(chēng)數(shù)組
$article->tagNames(); // get array of related tag names

// 獲取打了「任意」標(biāo)簽的 Article 對(duì)象
Article::withAnyTag("Gardening, Cooking")->get(); // fetch articles with any tag listed
Article::withAnyTag(["Gardening","Cooking"])->get(); // different syntax, same result as above
Article::withAnyTag("Gardening","Cooking")->get(); // different syntax, same result as above

// 獲取打了「全包含」標(biāo)簽的 Article 對(duì)象
Article::withAllTags("Gardening, Cooking")->get(); // only fetch articles with all the tags
Article::withAllTags(["Gardening", "Cooking"])->get();
Article::withAllTags("Gardening", "Cooking")->get();

EstGroupeTaggableModelTag::where("count", ">", 2)->get(); // return all tags used more than twice

Article::existingTags(); // return collection of all existing tags on any articles

如果你 創(chuàng)建了 Tag.php,即可使用以下標(biāo)簽讀取功能:

// 通過(guò) slug 獲取標(biāo)簽
Tag::byTagSlug("biao-qian-ming")->first();

// 通過(guò)名字獲取標(biāo)簽
Tag::byTagName("標(biāo)簽名")->first();

// 通過(guò)名字?jǐn)?shù)組獲取標(biāo)簽數(shù)組
Tag::byTagNames(["標(biāo)簽名", "標(biāo)簽2", "標(biāo)簽3"])->first();

// 通過(guò) Tag ids 數(shù)組獲取標(biāo)簽數(shù)組
Tag::byTagIds([1,2,3])->first();

// 通過(guò)名字?jǐn)?shù)組獲取 ID 數(shù)組
$ids = Tag::idsByNames(["標(biāo)簽名", "標(biāo)簽2", "標(biāo)簽3"])->all();
// [1,2,3]
標(biāo)簽事件

Taggable trait 提供以下兩個(gè)事件:

EstGroupeTaggableEventsTagAdded;

EstGroupeTaggableEventsTagRemoved;

監(jiān)聽(tīng)標(biāo)簽事件:

Event::listen(EstGroupeTaggableEventsTagAdded::class, function($article){
    Log::debug($article->title . " was tagged");
});
單元測(cè)試

基本用例測(cè)試請(qǐng)見(jiàn): tests/CommonUsageTest.php。

運(yùn)行測(cè)試:

composer install
vendor/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/

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/21569.html

相關(guān)文章

  • 使用 Baum 嵌套集合模型來(lái)實(shí)現(xiàn) Laravel 模型的無(wú)限極分類(lèi)

    摘要:本文經(jīng)授權(quán)轉(zhuǎn)自社區(qū)使用嵌套集合模型來(lái)實(shí)現(xiàn)模型的無(wú)限極分類(lèi)說(shuō)明大家通常都是使用遞歸實(shí)現(xiàn)無(wú)限極分類(lèi),都知道遞歸效率很低,下面推薦一個(gè)的擴(kuò)展包,快速讓你的數(shù)據(jù)模型支持無(wú)限極樹(shù)狀層級(jí)結(jié)構(gòu),并且兼顧效率。 本文經(jīng)授權(quán)轉(zhuǎn)自 PHPHub 社區(qū) 使用 Baum 嵌套集合模型來(lái)實(shí)現(xiàn) Laravel 模型的無(wú)限極分類(lèi) 說(shuō)明 大家通常都是使用遞歸實(shí)現(xiàn)無(wú)限極分類(lèi),都知道遞歸效率很低,下面推薦一個(gè) Larav...

    superPershing 評(píng)論0 收藏0
  • Laravel:使用Migrations

    摘要:首先利用創(chuàng)建一個(gè)可遷移的數(shù)據(jù)表模板,該命令運(yùn)行后會(huì)在目錄下生成一個(gè)文件生成的文件包含和兩個(gè)方法,其中中是包含了添加表,添加列,添加索引等等一切的描述,比較簡(jiǎn)單,就是刪除表,當(dāng)然里面還可以有一些其他邏輯中支持的數(shù)據(jù)表列類(lèi)型,做個(gè)備注,暫時(shí) 1、首先利用artisan創(chuàng)建一個(gè)可遷移的數(shù)據(jù)表模板,該命令運(yùn)行后會(huì)在database/migrations目錄下生成一個(gè)文件 php artisan...

    waltr 評(píng)論0 收藏0
  • Laravel Telescope:優(yōu)雅的應(yīng)用調(diào)試工具

    摘要:文章轉(zhuǎn)自視頻教程優(yōu)雅的應(yīng)用調(diào)試工具新擴(kuò)展是由和開(kāi)源的應(yīng)用的調(diào)試工具。計(jì)劃任務(wù)列出已運(yùn)行的計(jì)劃任務(wù)。該封閉函數(shù)會(huì)被序列化為一個(gè)長(zhǎng)字符串,加上他的哈希與簽名如出一轍該功能將記錄所有異常,并可查看具體異常情況。事件顯示所有事件的列表。 文章轉(zhuǎn)自:https://laravel-china.org/topics/19013視頻教程:047. 優(yōu)雅的應(yīng)用調(diào)試工具--laravel/telesco...

    MasonEast 評(píng)論0 收藏0
  • 人人必備的10個(gè) Laravel 4 擴(kuò)展包

    摘要:更多擴(kuò)展包中有豐富的擴(kuò)展包來(lái)幫你完成幾乎任何你想實(shí)現(xiàn)的功能。我們不能把所有的擴(kuò)展包都整理出來(lái),然而,這里還是列出了一些很有用的??傊銕缀蹩偸悄軌蛘业揭粋€(gè)擴(kuò)展包可以解決你當(dāng)前的問(wèn)題。 Laravel 是一個(gè)非常流行且簡(jiǎn)單易用的PHP框架,它提供了很多基礎(chǔ)的工具(如 RESTful 路由、內(nèi)置的ORM、模版等)使你能夠快速的創(chuàng)建應(yīng)用。這意味著你可以花費(fèi)更少的時(shí)間來(lái)建立應(yīng)用程序的模版,給...

    darkbug 評(píng)論0 收藏0
  • 13 個(gè)快速構(gòu)建 Laravel 后臺(tái)的擴(kuò)展包

    摘要:值得一提的是擴(kuò)展包不免費(fèi)用于商業(yè)用途,作者用一種人類(lèi)友好的方式說(shuō)你使用這個(gè)擴(kuò)展包就是應(yīng)該去掙錢(qián)的,而不是免費(fèi)的去工作這個(gè)擴(kuò)展包收費(fèi)美元。除了這些,還有五個(gè)沒(méi)有全面的審查的擴(kuò)展包。最后,還有三個(gè)優(yōu)質(zhì)的包選擇于。 showImg(https://segmentfault.com/img/remote/1460000012312105?w=2200&h=1125); 開(kāi)發(fā)者們都是懶惰的,不,...

    MiracleWong 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

lucas

|高級(jí)講師

TA的文章

閱讀更多
最新活動(dòng)
閱讀需要支付1元查看
<