【发布时间】:2020-08-30 09:19:18
【问题描述】:
我有一个包含产品属性数组的集合,大多数属性是一个数组:[property_name, property_name_translated, value_of_property]
像这样:
Product {#290 ▼
#connection: "general"
#table: "product"
#primaryKey: "id"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:18 [▼
"id" => 2
"image" => "sensor.png"
"url" => "senso"
"battery" => array:2 [▼
"value" => "Rechargable (2h to fully charge / 40h autonomy)"
"property_trans" => "Battery"
]
"compatible" => array:2 [▼
"value" => "Andriod/iOS with Bluetooth 4.0, RookMotion Center/APP"
"property_trans" => "Compatible"
]
...
"height" => array:2 [▼
"value" => "4"
"property_trans" => "height"
]
"memory" => array:2 [▼
"value" => "Yes (8h)"
"property_trans" => "Memory"
]
]
#original: array:3 [▼
"id" => 2
"image" => "sensor.png"
"url" => "sensor"
]
#relations: []
#hidden: []
#visible: []
#appends: []
#fillable: []
#guarded: array:1 [▼
0 => "*"
]
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
}
在我的刀片上,我将产品作为实体 如果我在刀片中打印实体的内容,则属性在网页中:
<td class="border-none">{{$entidad}}</td>
打印:
{"id":2,"image":"sensor","url":"sensor","battery":{"value":"Rechargable (2h to fully charge \/ 40h autonomy)","property_trans":"Battery"},"compatible":{"value":"Andriod\/iOS with Bluetooth 4.0, RookMotion Center\/APP","property_trans":"Compatible"}, ... "weight":{"value":"70","property_trans":"weight"},"width":{"value":"6.4","property_trans":"width"},"price":"76.90"}
如何制作一个 for 循环来打印属性的每个元素?
手动打印每个元素都可以
// This works and shows the property name translated and its value
@if(!is_null($entidad->battery))
<td class="border-none">{{$entidad->battery['property_trans']}}</td>
<td class="border-none">{{$entidad->battery['value']}}</td>
@endif
但是 for 循环不能正常工作
<tbody>
@foreach($entidad as $property)
<tr>
<td>Found one prop</td>
<td>{{$property}}</td>
</tr>
@endforeach
</tbody>
它只打印集合的 4 个元素,但不打印集合属性
+incrementing: true
+timestamps: true
+exists: true
+wasRecentlyCreated: false
为什么我不能循环集合属性?在我的控制器中,我可以使用 foreach 访问所有属性,但在刀片中我不能。
谢谢
【问题讨论】:
标签: php foreach collections laravel-blade