简体   繁体   中英

Laravel search Json column for numbers between two values

I have a viewers JSON column in the products table that hold a similar value example:

[{"at": "2021-06-21T18:58:39", "age": 30, "country_iso_code": "US"}, {"at": "2021-06-18T11:13:13", "age": 20, "country_iso_code": "PH"}, {"at": "2021-06-25T23:57:12", "age": 45, "country_iso_code": "ET"}, {"at": "2021-06-27T17:01:14", "age": 18, "country_iso_code": "DZ"}

So, I'm trying to get rows that contain numbers between two JSON age property values, for example, age between 20-30 .

I have tried to use the eloquent method WhereJsonContains but it does only works with the exact number values (as far as I know):

$query->orWhereJsonContains('viewers', [
    'age' => 20
]);

this code will basically return the following query:

select * from `products` where (json_contains(`viewers`, '{\"age\":\"20\"}'))

Is there a way I can handle this to use the between? maybe a regex solution

you can do this.

return \App\Models\Product::where("viewers->age","<",30)->where("viewers->age",">",20)->get();

-> point to the object

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