获取所有必需数据的功能:
$auditResults = Audit::where('audit_id', $id)
->with('questionDetail')
->with('questionDetail.auditQuestion')
->get();
返回(简化):
Audit {#427 ▼
#relations: array:1 [▼
"questionDetail" => AuditQuestionDetail {#449 ▼
#relations: array:1 [▼
"auditQuestion" => AuditQuestion {#471 ▼
#original: array:5 [▶]
}
]
}
]
}
我如何在视图中循环,以便为每个auditQuestion
达到Audit
关系?
我试过了:
@foreach($auditResults->questionDetail->auditQuestion as $answer)
但我明白了:
未定义的属性: 照亮\数据库\雄辩\收藏:: $ questionDetail
非常感谢。
**第一关系问题:**
Collection {#470 ▼
#items: array:18 [▼
0 => Audit {#427 ▼
#fillable: array:4 [▶]
#attributes: array:7 [▶]
#original: array:7 [▶]
#relations: array:1 [▼
"questionDetail" => AuditQuestionDetail {#449 ▼
#table: "audit_questions_details"
#fillable: array:3 [▶]
#attributes: array:7 [▶]
#original: array:7 [▼
"id" => 2
"audit_question_id" => 2
"question_number" => 1
"comment" => 1
"header" => 0
"created_at" => "2017-03-26 13:40:18"
"updated_at" => "2017-03-26 13:40:18"
]
答案 0 :(得分:1)
使用嵌套循环:
@foreach ($auditResults as $result)
@foreach ($result->questionDetail as $detail)
@foreach ($detail->auditQuestion as $question)
{{ $question->id }}
@endforeach
@endforeach
@endforeach
答案 1 :(得分:1)
在Laravel 5.4中,您可以使用higher order messages干净地执行此操作:
@foreach($auditResults->map->questionDetail->map->auditQuestion as $answer)