How to use where clause on an object in laravel

Viewed 29

I want to use a where clause on an object. I have tried the below method

DB::table('user')
->where('account_id', 17)
->where('address', '{"city":{"zipcode":0,"code":0},"country":{"code":0,"pincode":0}}')
->delete();

Which is giving below error

HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts. (SQL: delete from "user".....

Can anyone help me to solve this?

Thanks.

1 Answers

I assume you stored json in the address field. Also maybe try ->get() first before deleting.

Your json string is invalid. Possible solution is to json_encode an array with your data.

$json = json_encode([
     "city" => [ 
         "zipcode" => 0,
         "code" => 0
     ], 
     "country" =>
         "code" => 0,
         "pincode" => 0
]);


$users = User::where('account_id', 17)
     ->where('address', $json)
     ->get();

# now do action with users or do delete(I don't know if u can delete a null result or if it throws err.)
Related