gpt4 book ai didi

php - Laravel - 循环遍历嵌套集合

转载 作者:行者123 更新时间:2023-12-05 05:21:21 25 4
gpt4 key购买 nike

获取所有必需数据的函数:

$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 [▶]
}
]
}
]
}

对于每个 Audit,我如何在 View 中循环以达到 auditQuestion 关系?

我试过:

@foreach($auditResults->questionDetail->auditQuestion as $answer)

但是我得到:

Undefined property: Illuminate\Database\Eloquent\Collection::$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"
]

最佳答案

在 Laravel 5.4 中,你可以使用 higher order messages干净地做到这一点:

@foreach($auditResults->map->questionDetail->map->auditQuestion as $answer)

关于php - Laravel - 循环遍历嵌套集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43029729/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com