Laravel通过嵌套数组循环

时间:2020-09-07 18:01:20

标签: json laravel cart

{
  "8": {
    "id": "8",
    "name": "Third Product",
    "price": 3000,
    "quantity": "1",
    "attributes": {
      "tax": 7.5,
      "shop": "Edificeweb",
      "image": "rp-4.jpg"
    },
    "conditions": []
  },
  "9": {
    "id": "9",
    "name": "The fourth",
    "price": 200,
    "quantity": "1",
    "attributes": {
      "tax": 7.5,
      "shop": "Edificeweb",
      "image": "product-2.jpg"
    },
    "conditions": []
  }
}

我将购物车作为json列存储在我的订单页面中...如何使用whereJsonContains ....

检查经过身份验证的用户

我尝试过

$Order=Order::whereJsonContains('cart->attributes->shop',Auth::user()->name)->get();

它返回空数组

1 个答案:

答案 0 :(得分:0)

如果您正在使用mysql,则应尝试以下操作:

$Order = Order::whereJsonContains('cart->attributes', ['shop' => Auth::user()->name])->first();

使用postgresql尝试:

$Order = Order::whereJsonContains('cart->attributes', [['shop' => Auth::user()->name]])->first();

如果区分大小写,则应尝试原始操作:

$Order = Order::whereJsonContains(DB::raw('lower("cart->attributes"::text)'),
    [["shop" => strtolower(Auth::user()->name)]])->first();

我建议您将不带数字的对象保存为指示符,如果要将带数字的对象保存为指示符(“ 9”,“ 8”),则应尝试:

$Order = Order::whereJsonContains('cart->"$[*].attributes"', ['shop' => Auth::user()->name])->first();

顺便说一句,我建议您先使用而不是使用get,但最好尝试一下,看看有什么方便;)