How to sum based on a condition

Posted 2 years ago by Khudadad

Hi, all. I want to sum a field in the database(via query builder) based on a condition (ie. date<6/11/2016) and don't know which way is the best. I'm doing like this, is there any other better way to do this.

    $income = DB::table('incomes')
            ->join('currencies', 'currencies.cur_id','=','incomes.currency_id')
            ->selectRaw('incomes.*, currencies.currency ,sum(incomes.total) as total')
            ->where('inc_date','>',$sd)->where('inc_date','<',$ed)->get();
        return view('report.report', compact('income','sd','ed'))->render();

thanks.

Please sign in or create an account to participate in this conversation.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.