【问题标题】:PHP Blade Foreach element in collection attribute集合属性中的 PHP Blade Foreach 元素
【发布时间】: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


    【解决方案1】:

    您应该首先使用toArray() 方法将您的集合转换为数组

    <tbody>                                                         
        @foreach($entidad->toArray() as $property)
            <tr>
               <td>Found one prop</td>
               <td>{{$property}}</td>
            </tr>
        @endforeach                                                             
    </tbody>
    

    【讨论】:

    • 谢谢,这部分工作。我的意思是,使用您的代码,我可以访问数组的第一级。现在我需要访问父数组的每个元素上的数组。但是我需要检查元素是否是数组 if(is_array) 因为我遇到了一个错误,因为数组的某些元素不是数组。
    猜你喜欢
    • 2011-08-03
    • 2012-04-29
    • 1970-01-01
    • 2020-04-12
    • 2013-08-27
    • 1970-01-01
    • 1970-01-01
    • 2021-11-10
    • 2012-06-14
    相关资源
    最近更新 更多