Php 为什么总是返回空数组

Php 为什么总是返回空数组,php,laravel,Php,Laravel,这是我的搜索查询代码 if ($request->has('search')) { $search = $request->get('search'); $households = Household::where('data->household_id', 'LIKE', '%$search%') ->orWhere('data->data->household->address', 'LIKE', '%' .$sear

这是我的搜索查询代码

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"
            },

你有问题吗?我需要帮助

假设您的表有两列,即house_id和data,其中data是一个JSON字段,那么您的查询应该如下所示:

Household::where('household_id', 'LIKE', '%' . $search . '%')
        ->orWhere('data->household->address', 'LIKE', '%' .$search .'%')
        ->orWhere('data->members', 'LIKE', '%' .$search.'%')
        ->get();

我不知道你为什么要把所有东西都放在另一个数据级别。我也不认为
data->members
可以根据您提供的结构以这种方式进行搜索。

这不是连接表和获取相关数据的方式。我很惊讶它没有抛出任何错误。
“%$search%”
无论如何都是一个文本字符串。在
$houses=househouse::all()
这个caseSo数据是mysql中的json类型吗?我很确定你的意思是存储/插入,而不是迁移。也许他在使用MongoDBI,根据你的代码更改了我的代码,但仍然得到了相同的结果。根据你提供的,如果$search与任何家庭ID或地址匹配,这应该匹配,但因为我不知道你的表中有什么以及$search包含什么,我无法验证。我提供了数据库数据,但仍然返回空数组。仅适用于家庭身份证。不,您提供了插入数据的代码,但未提供任何数据。实际上,甚至不是这样,您只是提供了一个数组。我想这是从商店的方法。请再次检查我的帖子