Function to get all the required data:
$auditResults = Audit::where('audit_id', $id)
->with('questionDetail')
->with('questionDetail.auditQuestion')
->get();
Which returns (streamlined):
Audit {#427 ▼
#relations: array:1 [▼
"questionDetail" => AuditQuestionDetail {#449 ▼
#relations: array:1 [▼
"auditQuestion" => AuditQuestion {#471 ▼
#original: array:5 [▶]
}
]
}
]
}
How can I loop within the view, to reach the auditQuestion relationship, for each Audit?
I have tried:
@foreach($auditResults->questionDetail->auditQuestion as $answer)
But I get:
Undefined property: Illuminate\Database\Eloquent\Collection::$questionDetail
Many thanks.
** Issue with first relationship: **
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"
]
In Laravel 5.4, you can use higher order messages to do this cleanly:
@foreach($auditResults->map->questionDetail->map->auditQuestion as $answer)
Use nested loops:
@foreach ($auditResults as $result)
@foreach ($result->questionDetail as $detail)
@foreach ($detail->auditQuestion as $question)
{{ $question->id }}
@endforeach
@endforeach
@endforeach
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With