andileong / taggi
此包最新版本(1.2)没有可用的许可证信息。
Laravel 框架。
1.2
2021-07-10 08:44 UTC
Requires
- php: >=8.0
- illuminate/database: >= v8.47.0
- illuminate/support: >= v8.47.0
Requires (Dev)
- orchestra/testbench: ^6.19
- phpunit/phpunit: ^9
This package is auto-updated.
Last update: 2024-09-15 21:35:38 UTC
README
Taggi --- 一个使模型标签变得简单的 Laravel 包
使用 TTD 构建,只是一个简单且小巧的包,可以使你的标签功能变得容易。
安装
你的机器上至少需要安装 PHP8。
composer require andileong/taggi
配置
你可以通过以下操作将配置文件拉取到自己的项目中:
php artisan vendor:publish --tag=taggi-config //pull in the config file
php artisan vendor:publish --tag=taggi-migrations //pull in the migration file
标签创建
你可以创建一个标签或者大量创建标签。
// create one tag
$tag = new \Andileong\Taggi\Models\Tag()
$tag->put('tag name');
//creating multiple tags
$data = ['name1','name2','name3'];
$tag->massPut($data);
when we massive create tag you can pass a tring or array
I will pharse the string to a array for you
$data = ['name1','name2','name3'];
$data = "tag,tag2,tag3";
$data = "tag|tag2|tag3";
by default the string separator is supported as below
'separator' => "[,|!:.]",
you can overwrite it in the config file
CAUTION the tag model is use $guarded = [];
so be sure validate the data before insert.
more information check out:
https://laravel.net.cn/docs/8.x/eloquent#mass-assignment
标签关系
你可以将模型标签关联到标签上,以构建多对多关系。
首先,在你的模型中使用我的标签特性
<?php
use Andileong\Taggi\Models\Taggi;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class TopicStubs extends Model
{
use HasFactory , taggi;
}
all the relationship and method is defined inside the trait;
来标记你的模型
you can tag by a tag model isntance
$tag = \Andileong\Taggi\Models\Tag::find(1);
$post = Post::find(1);
$post->tag($tag);
you can tag by a tag collection isntance
$tags = \Andileong\Taggi\Models\Tag::take(3)->get();
$post = Post::find(1);
$post->tag($tags);
you can tag by a array()
$tags = ['tag','tag2','tag3'];
$post = Post::find(1);
$post->tag($tags);
you can tag by a string
$tags = "php|codeigniter";
$post = Post::find(1);
$post->tag($tags);
by defauault when the tag method is triggered behind the scene I use laravel toggle, that means if the tag you pass is tagged already , it will be untag,
$post->tagCount(); //return how many tag currently have
$post->tagged($tag); //check if the a tag is tagged
$post->unTagOne($tag); //untag a tag
$post->unTagAll(); //remove al lthe tags a post have