0

I am trying to run a foreach loop to query data within a json data column in my DB. I believe the foreach code is correct, but I cant retrieve data using the loop. I trying to use eloquent queries if possible.

Data stored in column named 'figuresinorder' : ["1","22","2","45"]

            $items = ['1', '2', '3'];
            $figures3 = customtrades::where(function ($query) use ($items) {
                foreach($items as $item) {
                  $query->whereJsonContains('figuresinorder', "$item");
                }
            })
                ->get();

1 Answer 1

1

First try to remove the " surrounding $item,

$query->whereJsonContains('figuresinorder', $item);
 

If that doesn't work, try this instead:

$query->whereRaw('JSON_CONTAINS(figuresinorder, ?)', [$item]);
Sign up to request clarification or add additional context in comments.

3 Comments

I still have no joy with both statements, I it baffling me because if i change $item to a number e.g. ``` $query->whereJsonContains('figuresinorder', '1'); ``` I get a returned result, so I have no idea why using the variable is not working.
if you pass it through " ", it will get $item a string and no values have been changed
thanks for your feedback and help, I am not sure what you mean by pass through " ".

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.