0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

laravelタグ付け機能

Last updated at Posted at 2023-10-24

拡張しやすいように作成していく

DBテーブルを作成

Tagname

id,name,created_at,updated_at

Tag

id,foreign_key,model,tagname_id,created_at,updated_at

User

id,name,created_at,updated_at

テーブル完成OK

それぞれのモデルを作成して関連付け

Tagname.php
public function tag(){
    return $this->hasMany('App\Models\Tag')
        ->orderBy('updated_at', 'desc');
}
Tag.php
public function tagname()
{
    return $this->belongsTo('App\Models\Tagname');
}

public function user()
{
    return $this
        ->belongsTo('App\Models\User','foreign_key')
        ->orderBy('updated_at', 'desc');
}
User.php
public function tags(){
    return $this->hasMany('App\Models\Tag','foreign_key')
        ->where('model', 'User')
        ->orderBy('updated_at', 'desc');
}

タグのIDから、関連ユーザーも検索

hoge.php
$limit = 2;
$res = Tagname::query()
    ->with(
        [
            'tag.user','tag' => function ($q) { $q->where('model', 'User'); }
        ]
    )
    ->where('id',105)
    ->orderBy('id', 'desc')
    ->paginate($limit);//paginateで統一しておくか。件数指定

    foreach ($res as $v) {
        echo "タグ名" . $v->name."<br>";
        foreach ($v->tag as $s) {
            if(!empty($s->user->name)){
                echo $s->user->name;
            }
        }
    }

//出力
//タグ名体験談
//かな
//ちゃんあみ

ユーザーから、関連付けられているタグも取得

hoge.php
$limit = 2;
$res = User::query()//単数形 User
        ->with('tags.tagname')
        ->orderBy('id', 'desc')
        ->paginate($limit);//paginateで統一しておくか。件数指定


foreach ($res as $v) {
    echo $v->name."<br>";
    foreach ($v->tags as $s) {
        echo "タグ : " . $s->tagname->name."<br>";
    }
}

dd($res);

//ちゃんあみ
//タグ : お知らせ
//タグ : 体験談
//hideki

タグ付け

ユーザーの登録時、変更時に、今までのタグを一旦すべて削除してから新規追加する。

hoge.php
$user_id = 296640923;
$model = 'User';

$tags = [
    'ウェブショップ',
    'お知らせ',
    '体験談'
];

$tagname_ids = [];

foreach ($tags as $v) {
    $tag = Tagname::query()//単数形 User
        ->where('name',$v)
        ->first();//最初の
    if($tag){
        //あればタグのIDを取得
        $tagname_ids[] = $tag['id'];
    } else {
        //なければ新規保存
        $model = new Tagname;
        $model->name = trim($v);
        $model->save();
        $tagname_ids[] = $model->id;
    }

}


$now =  Carbon::now();


$data = [];
foreach ($tagname_ids as $v) {

    $data[] = [
        'id' => null,
        'foreign_key' => $user_id,
        'model' => $model,
        'tagname_id' => $v,
        'created_at' => $now,
        'updated_at' => $now
    ];

}

$data = array_values($data);

Tag::query()
    ->where('foreign_key',$user_id)
    ->where('model',$model)
    ->delete();//削除


Tag::upsert($data, ['id']);//idを軸にすべてのフィールドを更新
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?