Im trying to do this.
The checkDailyCap function works perfect but the checkTotalCap function doesn’t. How do I only factor reports with a status of 2 in the results? Am I able to do foreach($campaign->reports as $report WHERE status == "2") ?
public static function checkTotalCap(Campaign $campaign)
{
$total_cap = 0;
foreach($campaign->reports as $report)
{
$total_cap = $total_cap + $report->rate;
}
if($campaign->cap <= $total_cap)
return true;
return false;
}
public static function checkDailyCap(Campaign $campaign)
{
$daily_cap = 0;
foreach($campaign->reports as $report)
{
// Check for reports today where status is 2
if(($report->created_at->isToday()) && $report->status == "2")
{
$daily_cap = $daily_cap + $report->rate;
}
}
if($campaign->daily_cap <= $daily_cap)
return true;
return false;
}
>Solution :
You can try this to filter the reports with status == 2 and:
$reports = $campaign->reports()->where('status', 2)->get();
foreach($reports as $report) {
//TODO
}