获取在 laravel 5.7 的列中具有指定值之一的所有行

Get all the rows that are having one of the specified value in their column in laravel 5.7

我的数据库 table 如下所示。

    Schema::create('x_poll_questions', function (Blueprint $table) {
            $table->engine = 'InnoDB';
            $table->increments('id');
            $table->string('question');
            $table->text('batch_ids');
            $table->timestamps();
        });

我有这样的数据,

1 |  test question 1 | ["1","4","2"]
2 |  test question 2 | ["1","5","3"]
3 |  test question 3 | ["1"]
4 |  test question 4 | ["3", "2"]

batch_ids 列中,我的值类似于 ["1","4","2"]。这是 json 数组的 php 解码值。

现在,我如何编写查询来检查 batch_ids 列中是否存在指定值(示例 1,2)。如果是,则 return 该行。

需要输出为

1 |  test question 1 | ["1","4","2"]
4 |  test question 4 | ["3", "2"]

更新的答案

在轮询模型中添加以下功能对我有用。

    public function scopeFilterByBatchIds($q, $batchIds)
    {
        if ($batchIds) {
            $batches = explode(',', $batchIds);

            foreach ($batches as $batchId) {
                $q->where('batch_ids', 'like', '%"' . $batchId . '"%');
            }
        }

        return $q;
    }
    ```

查找 DB Query WhereIn,您需要相应地进行调整

$filter_apostrophe = str_replace('"','', $value);
$separate_the_value = explode(',', $filter_apostrophe);

$data = DB::table('x_poll_questions')
      ->where('batch_ids','like','%'."".$separate_the_value."".'%') //Filter "" or not
      ->get()

搜索字段中的输入格式是什么