PHP code example of secretwebmaster / wncms-tags

1. Go to this page and download the library: Download secretwebmaster/wncms-tags 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/ */

    

secretwebmaster / wncms-tags example snippets


// apply HasTags trait to a model
use Illuminate\Database\Eloquent\Model;
use Wncms\Tags\HasTags;

class Post extends Model
{
    use HasTags;

    // ...
}


// 創建你的模型,以Post為例
$post = Post::create([
   'name' => '文章標題',
]);

// 添加標籤
$post->attachTag('標籤1'); //不指定類型
$post->attachTag('標籤2', '文章標籤'); //指定類型
$post->attachTags(['標籤3', '標籤4']);  //同時創建多個,不指定類型
$post->attachTags(['分類1','分類2'], '文章分類'); //同時創建多個,指定類型,最常用

// 移除標籤
$post->detachTag('標籤1');
$post->detachTag('標籤2', '文章標籤');
$post->detachTags(['標籤3', '標籤4']);
$post->detachTags(['分類1', '分類2'], '文章分類');

// 取得模型的所有標籤
$post->tags;

// 同步標籤
$post->syncTags(['標籤1', '標籤2']); //其他標籤將被移除

// 同步帶有類型的標籤
$post->syncTagsWithType(['標籤1', '標籤2'], '文章標籤');
$post->syncTagsWithType(['紅色', '黑色'], '顏色');

// 獲取帶有類型的標籤
$post->tagsWithType('文章標籤');
$post->tagsWithType('顏色');

// 獲取帶有任意一個標籤的模型
Post::withAnyTags(['標籤1', '標籤2'])->get();

// 獲取帶有所有標籤的模型,需同時擁有所有
Post::withAllTags(['標籤1', '標籤2'])->get();

// 獲取不帶有指定標籤的模型
Post::withoutTags(['標籤1', '標籤2'])->get();

// 翻譯標籤
$tag = Tag::findOrCreate('我的標籤');
$tag->setTranslation('name', 'en', 'My Tag');
$tag->setTranslation('name', 'fr', 'Mon tag');
$tag->setTranslation('name', 'nl', 'Mijn tag');
$tag->save();

// 獲取翻譯
$tag->getTranslation('name'); //返回我的標籤名稱,使用當前語言
$tag->getTranslation('name', 'fr'); //返回 Mon tag (可選語言參數)

// 使用標籤類型
$tag = Tag::findOrCreate('標籤1', '文章標籤');
$tag = Tag::findOrCreate('tag_slug_1'); // slug 不會因語言設定而改變
$tag->slug; //返回 "tag_slug_1"
bash
php artisan vendor:publish --provider="Wncms\Tags\TagsServiceProvider" --tag="tags-migrations"
bash
php artisan migrate