数据库表如下:
数据库中的“ extras”列存储着一个看起来像这样的对象数组:
[
{"id":92,"product_id":8966,"extra_type":"Extras","extra_name":"Olives"},
{"id":93,"product_id":8966,"extra_type":"Extras","extra_name":"Ketchup"},
{"id":92,"product_id":8966,"extra_type":"Extras","extra_name":"Olives"}
]
下面的代码(来自Laravel刀片)返回对象数组。
@foreach($item->orderedProducts as $op)
<tr>
<td>
<span>{{ $op['extras'] }}</span><br>
</td>
</tr>
@endforeach
$ op返回此:
{
"id": 171,
"product_id": 8966,
"order_id": 175,
"price": 11,
"count": 1,
"product_data": "{\"id\":8966,\"name\":\"Camera Br\\u00fbl\\u00e9e\",\"description\":\"Lorem ipsum dolor sit amet consectetur adipiscing elit etiam, conubia tempus sed dapibus augue gravida accumsan. Odio congue in blandit iaculis risus gravida parturient dictum quis rhoncus volutpat ornare tincidunt, dignissim ut pellentesque.\",\"price\":11,\"price_old\":null,\"category_id\":4584,\"created_at\":\"2019-03-17 14:59:56\",\"updated_at\":\"2019-04-17 16:37:03\",\"tax_group_id\":null,\"sort\":1,\"vendor_id\":null,\"option1\":null,\"option2\":null,\"option3\":null,\"option4\":null,\"option5\":null,\"option6\":null,\"images\":[\"http:\\/\\/localhost:8000\\/product_images\\/ElI9GImttc.jpg\"],\"formatted_price\":\"\\u00a311\",\"formatted_old_price\":\"0\",\"tax_value\":0,\"city_id\":null,\"restaurant_id\":null,\"product_images\":[{\"id\":9000,\"image\":\"\\/product_images\\/ElI9GImttc.jpg\",\"product_id\":8966,\"created_at\":\"2019-04-08 15:16:53\",\"updated_at\":\"2019-04-08 15:16:53\"}],\"tax_group\":null,\"category\":{\"id\":4584,\"name\":\"Random Things\",\"_lft\":1,\"_rgt\":2,\"parent_id\":null,\"created_at\":\"2018-11-09 13:15:01\",\"updated_at\":\"2019-04-17 16:47:21\",\"restaurant_id\":null,\"city_id\":null,\"category_image\":null,\"has_children\":0,\"image_url\":\"http:\\/\\/localhost:8000\\/category_images\\/a64be5a696402b0fe3649536ab6a49e4_1555519641.jpg\"},\"added\":true}",
"extras": "[{\"id\":93,\"product_id\":8966,\"extra_type\":\"Extras\",\"extra_name\":\"Ketchup\",\"extra_price\":\"1.20\",\"extra_added\":true,\"price_sum\":1.2,\"extra_count\":1}]",
"exclusions": "[{\"id\":117,\"product_id\":8966,\"extra_type\":\"Exclusions\",\"extra_name\":\"Rat poison\",\"extra_price\":null,\"exclusion_added\":true}]",
"created_at": "2019-04-27 10:35:11",
"updated_at": "2019-04-27 10:35:11",
"product": {
"id": 8966,
"name": "Camera Brûlée",
"description": "Lorem ipsum dolor sit amet consectetur adipiscing elit etiam, conubia tempus sed dapibus augue gravida accumsan. Odio congue in blandit iaculis risus gravida parturient dictum quis rhoncus volutpat ornare tincidunt, dignissim ut pellentesque.",
"price": 11,
"price_old": null,
"category_id": 4584,
"created_at": "2019-03-17 14:59:56",
"updated_at": "2019-04-17 16:37:03",
"tax_group_id": null,
"sort": 1,
"vendor_id": null,
"option1": null,
"option2": null,
"option3": null,
"option4": null,
"option5": null,
"option6": null,
"images": [
"http://localhost:8000/product_images/ElI9GImttc.jpg"
],
"formatted_price": "£11",
"formatted_old_price": "0",
"tax_value": 0,
"city_id": null,
"restaurant_id": null,
"product_images": [
{
"id": 9000,
"image": "/product_images/ElI9GImttc.jpg",
"product_id": 8966,
"created_at": "2019-04-08 15:16:53",
"updated_at": "2019-04-08 15:16:53"
}
],
"tax_group": null,
"category": {
"id": 4584,
"name": "Random Things",
"_lft": 1,
"_rgt": 2,
"parent_id": null,
"created_at": "2018-11-09 13:15:01",
"updated_at": "2019-04-17 16:47:21",
"restaurant_id": null,
"city_id": null,
"category_image": null,
"has_children": 0,
"image_url": "http://localhost:8000/category_images/a64be5a696402b0fe3649536ab6a49e4_1555519641.jpg"
}
}
}
我想要实现的是从“ extras:”中获取“ extra_name”的所有值。
所需的输出应类似于:
橄榄 番茄酱 橄榄
答案 0 :(得分:1)
@foreach($item->orderedProducts as $op)
<tr>
<td>
@if ('Extras' == $op->extra_type)
<span>{{ $op->extra_name }}</span><br>
@endif
</td>
</tr>
@endforeach
答案 1 :(得分:1)
我认为这会锻炼
@foreach($item->orderedProducts as $op)
<?php $array = stripslashes(json_encode($op->extras)) ?>
<tr>
<td>
<span>
@foreach($array as $value)
{{ $value['extra_name'] }}
@endforeach
</span><br>
</td>
</tr>
@endforeach
答案 2 :(得分:0)
您可以在laravel中使用->
来访问对象值,请尝试
@foreach($item->orderedProducts as $op)
<tr>
<td>
<span>{{ $op->extra_name }}</span><br>
</td>
</tr>
@endforeach
答案 3 :(得分:0)
我解决了。我需要的是在遍历json对象数组时使用json_decode()
。接下来,我提取了所需的属性。
@foreach($item->orderedProducts as $op)
<tr>
<td>
@foreach(json_decode($op->extras, true) as $extra)
{{ $extra['extra_name'] }}<br>
@endforeach
</td>
</tr>
@endforeach