I have two models, Email and EmailReview. Email hasMany EmailReviews. This query:
$data = Email::with('emailReviews')->where('created_by', '=', $personId)->get();
returns a collection of a specific person's emails
that have a review_group_type
of either approval
or feedback
and a relationship of email_reviews
that have an approved property of either true, false or null. I'm looking for the cleanest way to check if the email is an approval email and all of the reviews in the collection have their approved column set to true in order to set an approval status for each email i.e., $email->approvalStatus
. Some emails will also not have reviews yet, so I'll need to check for that. Something along these lines, but it's clearly getting ugly and I'm having a hard time getting my end result:
if ($data->review_group_type === 'approval') {
foreach($data as $email) {
if (!is_null($email->email_reviews))
foreach($email->email_reviews as $review) {
}
}
}