我有此属性表
+------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| user_id | int(11) | NO | | NULL | |
| meta_name | varchar(255) | NO | | NULL | |
| meta_value | text | NO | | NULL | |
| meta_group | varchar(255) | NO | | NULL | |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
+------------+------------------+------+-----+---------+----------------+
我检索所有值并将它们按meta_group
控制器
public function properties()
{
$properties = Properties::all()->groupBy('meta_group');
return view('dashboard.properties.index')->with('properties', $properties);
}
$properties
包含:
=> Illuminate\Database\Eloquent\Collection {#2953
all: [
"points" => Illuminate\Database\Eloquent\Collection {#2955
all: [
App\Properties {#2964
id: 1,
user_id: 3,
meta_name: "point_interval",
meta_value: "1",
meta_group: "points",
created_at: null,
updated_at: null,
},
App\Properties {#2963
id: 2,
user_id: 3,
meta_name: "point_amount",
meta_value: "1",
meta_group: "points",
created_at: null,
updated_at: null,
},
],
},
"message" => Illuminate\Database\Eloquent\Collection {#2949
all: [
App\Properties {#2965
id: 3,
user_id: 3,
meta_name: "message_length",
meta_value: "10",
meta_group: "message",
created_at: null,
updated_at: null,
},
App\Properties {#2966
id: 4,
user_id: 3,
meta_name: "message_score",
meta_value: "90",
meta_group: "message",
created_at: null,
updated_at: null,
},
],
},
],
}
在刀片服务器中,我这样做
@foreach($properties as $group=>$property)
{{$group}}
@endforeach
会输出points
,然后输出messages
,就像我期望的那样。
然后我要输出键名和值,例如meta_name:message_score
但是当我这样做
@foreach($property as $key=>$prop)
{{$prop}}
@endforeach
$prop
输出2个对象
{
"id": 1,
"user_id": 3,
"meta_name": "point_interval",
"meta_value": "1",
"meta_group": "points",
"created_at": null,
"updated_at": null
} {
"id": 2,
"user_id": 3,
"meta_name": "point_amount",
"meta_value": "1",
"meta_group": "points",
"created_at": null,
"updated_at": null
}
因此,我尝试获取每个键的名称
@foreach($prop as $ke => $pr)
{{$ke}}
@endforeach
但是输出
incrementing exists wasRecentlyCreated timestamps incrementing exists wasRecentlyCreated timestamps
和{{$pr}}
输出1 1 1 1 1 1
foreach()
出错了,我再也无法接受了。
如何打印键名称和值?
答案 0 :(得分:0)
我知道了。
我可以这样做来获取普通数组而不是集合,然后它可以工作
$properties = Properties::all()->groupBy('meta_group')->toArray();