1

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?

3
  • I'm not sure of the syntax for searching all storage objects' .quantity values, but Laravel does have support for JSON where clauses: laravel.com/docs/8.x/queries#json-where-clauses; might find something useful there 🙂 Commented Jan 20, 2022 at 16:04
  • I went through Laravel's JSON documentation, and it does not help with my problem. I used some of that information for other parts of my project, but the one I'm asking about here is not in any way covered by that documentation. But thanks anyway. Commented Jan 20, 2022 at 16:47
  • No problem! I was hopeful, but alas. Hopefully someone else will be able to help. Cheers! Commented Jan 20, 2022 at 16:59

1 Answer 1

1

We have whereJsonContains method for eloquent to search from json data in column..

Search in Json column with Laravel

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.