0

I have a column name test in which I am storing a JSON data and the data is like this

{
    "id": 4030662213806,
    "buyer_accepts_marketing": true,
    "cancel_reason": null,
    "cancelled_at": null,
    "line_items": [
        {
            "id": 10357822685358,
            "vendor": "Samsung-1",
        },
        {
            "id": 10357822685358,
            "vendor": "Samsung-2",
        },
        {
            "id": 10357822685358,
            "vendor": "Samsung-3",
        }
    ],
    
}

I am trying to get data where vendor value is Samsung-1

and my query is like this

$query = Test::where('id','4030662213806')
$query->orWhereJsonContains('test->line_items->vendor',$val);

also tried

$query->orWhereJsonContains('test->line_items->vendor',$val);

But didn't work

Any help please

4
  • I think you do not need a test on there. line_items->vendor Commented Oct 13, 2021 at 15:46
  • As you can see line_items is an array so will only line_items->vendor work ? I also tried like line_items[0]->vendor line_items[1]->vendor Commented Oct 13, 2021 at 15:52
  • According to your code the test is your table name but you say you have also a column named test I think you have a mistake here. Commented Oct 13, 2021 at 15:55
  • Ok lets assume table name is Order I added test only for example Commented Oct 13, 2021 at 16:17

2 Answers 2

1

Query where line_items array contains the key vendor and value Samsung-1

$query->orWhereJsonContains('test->line_items', ['vendor' => $val]);


Also if you are trying to query id from the test JSON column the first line should be

$query = Test::where('test->id', 4030662213806);
Sign up to request clarification or add additional context in comments.

Comments

0

you can try

$query = Test::where('id','10357822685358')
$query->orWhereJsonContains('test->line_items->vendor',$val);

Because it's id and value samsung 1

1 Comment

The first Test::where is just an example its the actual query I want to know is the condition in the orWhereJsonContains

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.