Php 其中JSON包含不返回任何内容
我有以下代码块Php 其中JSON包含不返回任何内容,php,laravel,Php,Laravel,我有以下代码块 try { $data = Checklist::where('setupStatus', true)->whereJsonContains('workspaceRegions', $regionID)->orderBy('id', 'desc')->paginate(5); return response()->json([ 'data' => $data
try {
$data = Checklist::where('setupStatus', true)->whereJsonContains('workspaceRegions', $regionID)->orderBy('id', 'desc')->paginate(5);
return response()->json([
'data' => $data
], 200);
} catch (\Throwable $th) {
return $th;
}
这将按预期工作并返回正确的检查列表
然后我在下一个块中也做了同样的操作,但这次我必须遍历和数组以获得要搜索的id值。但这始终返回一个空数组
$company = Auth::user()->company;
try {
$hodIDs = CompanyRoles::where('hod', true)->get();
$hods = [];
foreach ($hodIDs as $option) {
$id = strval($option->id);
$hods = User::where('companyID', $company->id)->whereJsonContains('companyRoles', $id)->with('employee')->get();
}
return response()->json([
'message' => 'success',
'hods' => $hods
], 200);
} catch (\Throwable $th) {
return $th;
}
“companyRoles”列是一个数组,包含类似[“1”、“2”]
如何返回companyRoles列数组中的每个用户和hodID
下面给出了我想要的,但这感觉有点像黑客
$hods = [];
foreach ($hodIDs as $option) {
$id = strval($option->id);
$user = User::where('companyID', $company->id)->whereJsonContains('companyRoles', $id)->with('employee')->first();
if ($user) {
array_push($hods, $user);
}
}
提前谢谢你你能给我看看你查询返回的SQL吗?嘿,我得到这个hods:Array(4)0:{id:1,角色:“高级HSE经理”,hod:1}1:{id:3,角色:“首席执行官”,hod:1}2:{id:28,角色:“项目经理”,hod:1}3:{id:31,角色:“hod”,hod:1}对问题进行了编辑,添加了我提出的解决方案,但我相信可以做得更好