此查询非常接近,但它根据最近的评论将approvalStatus
设置为true,而我想检查所有评论,并且只有所有评论都设置为真设置为true的approver_status
已批准该电子邮件。电子邮件可以有很多评论,一些评论者是批准者,有些只能提供反馈,但不能批准/拒绝。这是当前的查询:
$emailsWithReviews = Email::with('emailReviews')
->where(['created_by' => $personID, 'sent_at' => null])
->get()
->map(function ($email) {
foreach ($email->emailReviews as $review) {
if ($review->approver_status === true && $review->approved === false) {
return $email->approvalStatus = false;
}
$email->approvalStatus = true;
}
return $email;
});
我认为foreach会照顾这个,但如果最近的是$review->approver_status == 1 && $review->approved == 1
,它肯定会回归。
答案 0 :(得分:1)
我认为这可能有用,你几乎和foreach在一起,但是不要使用foreach,而是尝试使用filter()
返回任何已被拒绝的有效评论,并查看最终集合是否为空。
例如,如果您有效审核者进行了2次审核,则会接受一次拒绝,找到所有已拒绝的评论,并返回是否已找到任何评论。
Email::with('emailReviews')
->where(['created_by' => $personID, 'sent_at' => null])
->get()
->filter(function ($email) {
return $email->emailReviews->filter(function ($review) {
return $review->approver_status == 1
&& $review->approved == 0;
})->isEmpty();
})
->each(function ($email) {
$email->approvalStatus = true;
// Optionally:
$email->save();
});
或者如果你担心表现:
$ids = Email::with('emailReviews')
->where(['created_by' => $personID, 'sent_at' => null])
->get()
->filter(function ($email) {
return $email->emailReviews->filter(function ($review) {
return $review->approver_status == 1
&& $review->approved == 0;
})->isEmpty();
})->pluck('id');
Email::whereIn('id', $ids)->update([
'approvalStatus' => 1,
]);
我也很想将where条件提取到范围内,以使其更容易阅读:
Email::with('emailReviews')
->whereAuthor($personID)
->whereNotSent()
...
答案 1 :(得分:0)
好吧,我认为问题在于我是菊花链接到许多陈述。我通过分解来解决这个问题:
$emailsWithReviews = Email::with('emailReviews')
->whereNull('sent_at')
->get();
$emailsWithReviews->filter(function ($email) {
return $email->emailReviews->filter(function ($review) {
return $review->approver_status == 1
&& $review->approved == 0;
})->isEmpty();
})
->each(function ($email) {
$email->approvalStatus = true;
});