muan / laravel-tags
Laravel 标签包
0.1.0
2019-05-04 14:45 UTC
Requires
- php: >=7.0
This package is auto-updated.
Last update: 2024-09-08 18:17:51 UTC
README
用于创建标签。
需求
- "php": ">=7.0"
安装
- 在您的终端中输入以下命令
composer require muan/laravel-tags
- 将服务提供者添加到您的 config/app.php 文件中的 providers 部分
Laravel 5.5 使用包自动发现,因此不需要您手动添加 ServiceProvider。
'providers' => [ // ... Muan\Tags\Providers\TagsServiceProvider::class, // ... ],
- 在您的模型上使用以下特性
// Use trait use Muan\Tags\Traits\Taggable; class Post extends Model { use Taggable; // ... }
用法(示例)
创建并附加单个标签
// Create tag $tag = Muan\Tags\Model\Tag::create([ 'title' => $title = 'Laravel', 'slug' => str_slug($title) ]); // Attach tag $post = App\Post::find(1); $post->tags()->attach($tag->id);
创建并附加多个标签
$tags = ['Laravel', 'Eloquent', 'Tags']; foreach ($tags as $index => $tag) { $preparedTag = [ 'title' => $tag, 'slug' => str_slug($tag) ]; $tagId = Muan\Tags\Model\Tag::create($prepareTag)->id; $tags[$index] = $tagId; } $post = App\Post::find($id = 1); $post->tags()->sync($tags);
获取标签
$tags = App\Post::find(1)->tags;
命令
创建新标签
// Slug is generated automatically using str_slug() php artisan tag:create "tag title"
许可
Muan Laravel Admin 包使用MIT 许可协议。