Laravel 对象的属性名称
properties name of an Laravel Object
我在 Laravel 中有这个对象:
array:144 [▼
0 => {#10 ▼
+"year": 2005
+"month": "ene"
+"FOO": "37"
+"BAR": "17"
}
1 => {#11 etc...
我需要将名称 "year"、"month"、"FOO" 和 "BAR" 放在 table:
中
<thead>
<tr>
@foreach($data as $column_name)
<th><strong>{{ $column_name }}</strong></th>
@endforeach
</tr>
</thead>
$data 是对象。
所以 table 应该看起来像:
year | month | FOO | BAR
------------------------
2005 ene 37 17
这些值工作正常,但我不知道如何检索属性的名称来构建
编辑:
这是我根据一些答案得到的:
试试这个:
<thead>
<tr>
@foreach($data as $key)
<th><strong>{{ $key['year'] }}</strong></th>
@endforeach
</tr>
</thead>
更新:
<thead>
<tr>
@foreach($data as $key)
<th><strong>{{ $key->year }}</strong></th>
@endforeach
</tr>
</thead>
如果 $data
是 array
中的第一个 stdClass
对象,那么您应该能够执行以下操作:
<thead>
<tr>
@foreach((array) $data[0] as $key => $value)
<th><strong>{{ $key }}</strong></th>
@endforeach
</tr>
</thead>
我在 Laravel 中有这个对象:
array:144 [▼
0 => {#10 ▼
+"year": 2005
+"month": "ene"
+"FOO": "37"
+"BAR": "17"
}
1 => {#11 etc...
我需要将名称 "year"、"month"、"FOO" 和 "BAR" 放在 table:
中<thead>
<tr>
@foreach($data as $column_name)
<th><strong>{{ $column_name }}</strong></th>
@endforeach
</tr>
</thead>
$data 是对象。
所以 table 应该看起来像:
year | month | FOO | BAR
------------------------
2005 ene 37 17
这些值工作正常,但我不知道如何检索属性的名称来构建
编辑:
这是我根据一些答案得到的:
试试这个:
<thead>
<tr>
@foreach($data as $key)
<th><strong>{{ $key['year'] }}</strong></th>
@endforeach
</tr>
</thead>
更新:
<thead>
<tr>
@foreach($data as $key)
<th><strong>{{ $key->year }}</strong></th>
@endforeach
</tr>
</thead>
如果 $data
是 array
中的第一个 stdClass
对象,那么您应该能够执行以下操作:
<thead>
<tr>
@foreach((array) $data[0] as $key => $value)
<th><strong>{{ $key }}</strong></th>
@endforeach
</tr>
</thead>