In my Laravel project, I have a products table with a json column calle data. In the json column I have the following structure:
{
"storage" : [
{
"stowage" : 1,
"quantity" : 100
},
{
"stowage" : 5,
"quantity" : 500
}
]
}
I want to search products with stowages that have less than 500 items. I know that I can do this:
SELECT * FROM products p WHERE
JSON_EXTRACT( p.`data`, '$.storage[0].quantity') < 500;
First I would like to convert that to Laravel eloquent, something like:
$products = Product::where( 'data->storage[0].quantity, '<', 500 )->get();
I tried that one and does not work, but I would also want to search in all the stowages:
$products = Product::where( 'data->storage->>quantity', '<', 500 )->get();
Is it possible?
storageobjects'.quantityvalues, but Laravel does have support for JSON where clauses: laravel.com/docs/8.x/queries#json-where-clauses; might find something useful there 🙂