如何使用LARAVEL中的where条件在SQL中检索Json数据类型数据

问题描述 投票:1回答:1

问题:如何使用LARAVEL中的where条件在SQL中检索Json数据类型数据?

我要显示包含订单的所有订单->产品->用户-> id === 1

{
"currentUserID": 1,
"currentUserName": "Mohamed Naalir",
"order": [
    {
        "id": 26,
        "Product": [
            {
                "id": 4,
                "name": "Araliya Rice",
                "desription": "Araliya Rice",
                "salePrice": 500,
                "category": "Rice",
                "user": {
                    "id": 1,
                    "name": "Mohamed Naalir",
                }
            }
        ],
    },
    {
        "id": 27,
        "Product": [
            {
                "id": 2,
                "name": "white sugar",
                "desription": "aaa",
                "salePrice": 100,
                "category": "Sugar",
                "user": {
                    "id": 5,
                    "name": "Mohamed Sharaf",
                }
            }
        ],
    }
]

}

laravel laravel-5 eloquent laravel-6 laravel-7
1个回答
0
投票

json where子句

 $orders = DB::table('orders')
            ->whereJsonContains('Product', [['user' => ['id' => 1]]])
            ->get();
© www.soinside.com 2019 - 2024. All rights reserved.