[英]why this always return empty array
这是我的搜索查询代码
if ($request->has('search'))
{
$search = $request->get('search');
$households = Household::where('data->household_id', 'LIKE', '%$search%')
->orWhere('data->data->household->address', 'LIKE', '%' .$search .'%')
->orWhere('data->data->members', 'LIKE', '%' .$search.'%')->get();
} else {
$households = Household::all();
}
if ($households)
{
return response()->json([
'error' => false,
'data' => HouseholdResource::collection($households),
], 200);
}
return "No data found for search";
它总是返回空数组,即使数据存在于数据库中。
这是我的迁移
$data = [
'household_id' => $request->get('enumeration_id'),
'data' => json_encode([
'household' => [
'address'=> $request->get('address'),
],
'members'=> [],
]),
];
有我的迁移表
Schema::create('households', function (Blueprint $table) {
$table->uuid('id');
$table->primary('id');
$table->string('household_id')->unique();
$table->jsonb('data');
$table->timestamps();
$table->softDeletes();
});
数据库数据就像
"household": {
"enumeration_id": "1938347-32960066",
"location": "676 Derick Cape\nReeseburgh, WA 95751",
"identifier": "122 b 2",
"location_code": "LK-CMB-002",
"address": {
"text": "86242 Lynch Roads\nSouth Deon, KS 16600-5109",
"village": "address village",
"district": "address district"
},
你能解决问题吗? 我需要帮助
假设您的表有两列,family_id 和 data,其中 data 是一个 JSON 字段,您的查询应如下所示:
Household::where('household_id', 'LIKE', '%' . $search . '%')
->orWhere('data->household->address', 'LIKE', '%' .$search .'%')
->orWhere('data->members', 'LIKE', '%' .$search.'%')
->get();
我不确定您为什么要尝试将所有内容都放在另一个数据级别下。 我也不认为data->members
可以根据您提供的结构以这种方式进行搜索。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.