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( as total')
        return view('', compact('income','sd','ed'))->render();


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


Reply to

Use Markdown with GitHub-flavored code blocks.