ehabafia
1 month ago

I am trying to get total amount conditional and per month

Posted 1 month ago by ehabafia

Hello Laracasts,

I am trying to get a sum amount per month through created_at, these amounts are all positives but they have another field type which is either debit or credit.

So, either I need one sum but by flipping the sign of the debit to be negative and then add the amount together or by having sum fields one for credit and one for debit. These fields need to be per month.

so, the end result should be January or 1, 2020 - amount = 2000 ... or January or 1, 2020 - type = credit - total_credit = 3000 - type = debit - total_debit = 1000 ...

Something in that range.

    public function monthly_totals()
    {
        return $this->hasMany(Transaction::class)
            ->select('created_at', 'sum(amount)')
            ->groupBy('MONTH(created_at)', 'YEAR(created_at)')
            ->get();
    }

That is not even close to the solution, but kind of start.

Thank you so much for your support.

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