Home > Mobile >  Laravel 8 query json data from database
Laravel 8 query json data from database

Time:10-14

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

CodePudding user response:

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);

CodePudding user response:

you can try

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

Because it's id and value samsung 1

  • Related