LoginSignup
0
0

More than 3 years have passed since last update.

【Laravel】MySQL5.6→JSON形式のデータ→曖昧検索

Last updated at Posted at 2019-10-29

前提条件

テーブル:favorite_fruit

id data
1 {"fruit":["apple", "banana", "orange"]}
2 {"fruit":["plum", "pear"]}

検索ワード

$keywords = ['banana', 'melon'];

MySQL5.6

Laravel

$fruit = DB::table('favorite_fruit')
    ->where(function ($sub) use ($keywords) {
        foreach ($keywords as $keyword) {
            $sub->orwhere('favorite_fruit.data', 'like', '%"fruit":[%'. '"'. $keyword. '"'. '%]%');
        }
    });

最後に

MySQL5.7からはJSONが普通に使える

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