Support the ongoing development of Laravel.io →
Database Eloquent Architecture

Hello,

Just looking for an opinion here, as I can't get my head round what seems to be a trivial problem.

I have a transactions model which has the fields

ID
Date
Debit
Credit
Description

Debit and Credit both are decimals with amounts in.

I need to produce a statement for a given period in time (or display all transactions) showing the balance of the account.

I have thought of several methods - but none seem very optimised. I tried storing in the database but as transactions aren't stored in order (I could input a transaction from today and then one from a few weeks ago), I'd have to calculate a balance for every row every time I input a transaction, which could result in thousands of update queries.

I'm currently manually calculating the data in the view (incrementing or decrementing the balance and then displaying it in a foreach loop) but this is definitely bad practice and also doesn't help when I want to filter data (e.g only show data from April 2014)

Does anyone have any suggestions for what seems like a common issue?

Last updated 3 years ago.
0

A transaction can either be a debit or a credit not both at the same time.

So, why not this :

Table :  transactions

Schema :
id:increments
amount:decimal(x,y)
type:string or enum with debit / credit)
date:timestamp
timestamps: (for created_at & updated_at);
Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

AlexCutts1 alexcutts1 Joined 15 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.