Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What is the proper way to do for each where in laravel

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;
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading