Blog スタッフブログ

Laravel システム開発

[Laravel]カンマ区切りで保存されたデータから特定の値を検索する

システム開発担当のTFです。

やり方

  • クエリで whereRaw を用いて、素のwhere句を挿入する
  • SQLの FIND_IN_SET を用いて、カンマ区切りの文字列から、特定の値を検索する

サンプル

// Testのidsにカンマ区切りでidが格納されているとする。そこから指定idの入ったデータを取得する
$tests = Test::whereRaw('FIND_IN_SET(?, ids)', [$id])->get();

Warning: Undefined array key 0 in /home/fl0uhem6u4/mixltd.jp/public_html/cms/wp-content/themes/mix_theme/pagination-single.php on line 3

Warning: Attempt to read property "cat_ID" on null in /home/fl0uhem6u4/mixltd.jp/public_html/cms/wp-content/themes/mix_theme/pagination-single.php on line 3