简体   繁体   中英

why this always return empty array

this is my query code for search

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";

It always return empty array, even data exist in database.

and this is my migration

$data = [
        'household_id'   => $request->get('enumeration_id'),
        'data'           => json_encode([
            'household' => [
                'address'=> $request->get('address'),
            ],
            'members'=> [],

        ]),
    ];

there is my migration table

Schema::create('households', function (Blueprint $table) {
        $table->uuid('id');
        $table->primary('id');
        $table->string('household_id')->unique();
        $table->jsonb('data');
        $table->timestamps();
        $table->softDeletes();
    });

database data is like

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

Can you got the problem? i need help

Assuming your table has two columns, household_id and data, of which data is a JSON field, your query should look like:

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

I'm not sure why you're attempting to put everything under another data level. I also don't think data->members will be searchable in this manner based on your provided structure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM