我迷路了...我无法在数据库中获取我的数据...
所以我有一个名称为 preparation 和字段的表
ID,...,...,参与者,..,...。
我使用 json_encode 函数将另一个表的 ID 存储在参与者字段中,如下所示:['4','6','8']。
我想检索具有给定 ID 的所有结果。例如,如果我有:
ID---name---participants--------otherfield
1---Step1---['6','8']-----------lorem1
2---Step2---['4','7','8']-------lorem2
3---Step3---['2','3','6','9']---lorem3
我想检索所有编号为 6 的行,所以第 1 行和第 3 行。 我用 eloquent 或 querybuilder 做了几次测试,但没有结果。 例如我试过这个:
$idToFind = 6;
$query = PreparationStep::
whereRaw("JSON_CONTAINS(participants, ".$idToFind.")")
->get();
请帮帮我。
最佳答案
这是我在主题表中的 json 列
foreach ($order as $key => $value) {
$arr[]="$value->id";
} $arr=json_encode($arr);//生成动态 json 数组
$data=Subject::whereRaw('json_contains(course_id,\''.$arr.'\')')->get();
关于php - Eloquent - 在列中搜索 ID,例如 ['4' ,'6' ,'8' ] (PHP Laravel),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47679680/