这是我的搜索查询代码
const
它总是返回空数组,即使数据库中存在数据。
这是我的迁移
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();
});
您能解决问题吗?我需要帮助
答案 0 :(得分:0)
假设您的表有两列,Household_id和data,其中数据是JSON字段,则查询应类似于:
Household::where('household_id', 'LIKE', '%' . $search . '%')
->orWhere('data->household->address', 'LIKE', '%' .$search .'%')
->orWhere('data->members', 'LIKE', '%' .$search.'%')
->get();
我不确定您为什么要尝试将所有内容置于另一个数据级别下。我也不认为data->members
可以根据您提供的结构以这种方式进行搜索。