我设置了一个包含多个嵌入文档的MongoDB集合。这就是我的示例文档的样子:
{
"_id" : ObjectId("58a331ffb854d000f97862f3"),
"first_name" : "John",
"last_name" : "Doe",
"email" : "test@test.com",
"password" : "$2y$10$fIuECeTqvSUY1g.VPgWxceEzB0/q2OtgDXlm9ZTqwY77U74hVEe6q",
"updated_at" : ISODate("2017-02-14T16:36:15.079Z"),
"created_at" : ISODate("2017-02-14T16:36:15.079Z"),
"diagnosis" : [
{
"d_name" : "Asthma",
"d_isTreated" : "No",
"d_diagnosed_at" : "2017-02-14"
},
{
"d_name" : "Bronchitis",
"d_isTreated" : "No",
"d_diagnosed_at" : "2017-02-14"
},
{
"d_name" : "Hepatitis C",
"d_isTreated" : "No",
"d_diagnosed_at" : "2017-02-14"
}
]
}
父文档属于User
类。但是,在我的主页视图中,我尝试为每个用户检索诊断列表,如下所示:
<center>
@foreach($users as $user)
{{ $user->diagnosis }}
@endforeach
</center>
Laravel无法检索diagnosis
个对象的数组并输出错误:
ErrorException in helpers.php line 532:
htmlspecialchars() expects parameter 1 to be string, array given (View: /Users/janisozolins/Sites/patientapp/resources/views/home.blade.php)
如何检索这些数组?我还计划通过在每个Prescriptions
文档中添加Diagnosis
子文档来进一步嵌入它们。
答案 0 :(得分:0)
您需要遍历诊断数组。
@foreach($users as $user)
@foreach $diagnosis as $user->diagnosis
{{ var_dump($diagnosis) }}
@endforeach
@endforeach