mihakot / laravel-tags
为您的Laravel应用模型添加标签
v1.1.1
2024-05-12 12:26 UTC
Requires
- php: ^8.1
- laravel/framework: ^9.0 || ^10.0
- spatie/laravel-package-tools: ^1.16.0
Requires (Dev)
- orchestra/testbench: ^6.13 || ^7.0
- phpunit/phpunit: ^9.5.2
This package is auto-updated.
Last update: 2024-09-12 10:10:02 UTC
README
安装
您可以通过composer安装此包
composer require mihakot/laravel-tags
发布提供者配置
artisan vendor:publish --provider=Mihakot\Tags\TagsServiceProvider
用法
以下是一些代码示例
// apply HasTags trait to a model
use Illuminate\Database\Eloquent\Model;
use Mihakot\Tags\HasTags;
class ArticleItem extends Model
{
use HasTags;
// ...
}
// Create a model with tags
$articleItem = ArticleItem::create([
'name' => 'The Article Title',
'tags' => ['first tag', 'second tag'], //tags will be created if they don't exist
]);
// Attaching tags
$articleItem->attachTag('third tag');
$articleItem->attachTag('third tag','some_type');
$articleItem->attachTags(['fourth tag', 'fifth tag']);
$articleItem->attachTags(['fourth_tag','fifth_tag'],'some_type');
// Detaching tags
$articleItem->detachTags('third tag');
$articleItem->detachTags('third tag','some_type');
$articleItem->detachTags(['fourth tag', 'fifth tag']);
$articleItem->detachTags(['fourth tag', 'fifth tag'],'some_type');
// Get all tags of a model
$articleItem->tags;
// Syncing tags
$articleItem->syncTags(['first tag', 'second tag']); // all other tags on this model will be detached
// Retrieving models that have any of the given tags
ArticleItem::withAnyTags(['first tag', 'second tag'])->get();
// Retrieve models that have all of the given tags
ArticleItem::withAllTags(['first tag', 'second tag'])->get();
测试
composer test
变更日志
请参阅变更日志以获取更多最近更改的信息。
贡献
请参阅贡献指南以获取详细信息。
安全
如果您发现任何与安全相关的问题,请通过电子邮件mihakot@gmail.com联系,而不是使用问题跟踪器。
鸣谢
许可证
MIT许可证(MIT)。有关更多信息,请参阅许可证文件。
关键词
laravel, tags