Home > Back-end >  array search in database column array in laravel
array search in database column array in laravel

Time:12-15

My Amenity Database column Store json_encode(array) data like this:

column name amenity

[{"key":"4MiZ54lNosCWc5VPCBa5uGBwrinFGC","value":"WI_FI"},{"key":"fuDn4n5gSMFJ5hCOOp445dh5Cmfc5g","value":"COMPLIMENTARY BREAKFAST"}.....]

column name variable name $search_term

My Search Term in Array Like this:

[ "WI_FI", "TELEPHONE", "SWIMMING POOL" ]

I try bun not work :

$data->whereIn('amenity',$search_term)

CodePudding user response:

try this:

$search_term = ['WI_FI', 'COMPLIMENTARY '];
        $values = array_map(function($value) {
            return array(
                'value' => $value
            );
        }, $search_term);

$data = new yourModel();
        return $data->whereJsonContains('amenity', $values)
            ->get();

with array_map() you return a array with key 'value'.

CodePudding user response:

My Answer :

$_search_term = [ "WI_FI", "TELEPHONE", "SWIMMING POOL" ]

foreach ($search_term $_search_term){ $data->where('amenity', 'LIKE', "%{$_search_term}%"); }

  • Related