人気のPHP WEBアプリケーションフレームワークLaravelのTipsを記録していきます

Laravel のモデルにタグを追加するパッケージ rtconner/laravel-tagging を使用する

● Laravel のモデルにタグを追加するパッケージ rtconner/laravel-tagging を使用する

https://packagist.org/packages/rtconner/laravel-tagging


# ● モデルに以下を追加

例: /app/Post.php にトレイトを追加

    use \Conner\Tagging\Taggable;           // tag


● モデル取得時にタグのリレーションも同時に取得する

eager loadingされます

$post = \App\Post::with('tagged')->first();


● モデルに「タグを付ける」「タグを外す」「タグを更新する」

$post->tag('Gardening'); // attach the tag
$post->tag('Gardening, Floral'); // attach the tag
$post->tag(['Gardening', 'Floral']); // attach the tag

$post->untag('Cooking'); // remove Cooking tag
$post->untag(); // remove all tags

$post->retag(['Fruit', 'Fish']); // delete current tags and save new tags
$post->retag('Fruit', 'Fish');
$post->retag('Fruit, Fish');
No.2029
08/12 08:53

edit