JSON解码 - 解码购物车PHP

时间:2017-06-13 13:26:37

标签: php json laravel

您好我在Laravel 5.4工作。我用json_encode将我的购物车保存到数据库,这没问题。我的问题是在我的视图中获取我想要的数据。我想举例说明产品的标题和价格。

我试过了:

Trying to get property of non-object

这是我收到下一个错误的时候:

dd(json_decode($order->cart, true))

当我转储购物车时

array:3 [▼
  "items" => array:1 [▼
    5 => array:4 [▼
      "id" => 5
      "qty" => 1
      "price" => 15
      "item" => array:8 [▼
        "id" => 5
        "created_at" => "2017-06-05 13:21:45"
        "updated_at" => "2017-06-05 13:21:45"
        "imagePath" => "https://someImage"
        "title" => "Vitamin B-12"
        "discription" => "Vitamin B-12, also called cobalamin ▶"
        "category" => 4
        "price" => 15
      ]
    ]
  ]
  "totalQty" => 1
  "totalPrice" => 15
]

我明白了:

disabled

1 个答案:

答案 0 :(得分:0)

您的$order->cart是一个数组而不是一个对象。 试试这个:

<?php $data = json_decode($order->cart, true); ?>
@foreach ($data['items'] as $item)
    {{ $item['item']['title'] }} {{ $item['item']['price'] }}
@endforeach