PHPackages                             wzulfikar/eloquent-simple-ledger - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. wzulfikar/eloquent-simple-ledger

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

wzulfikar/eloquent-simple-ledger
================================

1.1.3(10y ago)242.5k↓33.3%15[1 issues](https://github.com/wzulfikar/eloquent-simple-ledger/issues)PHP

Since Apr 11Pushed 10y ago6 watchersCompare

[ Source](https://github.com/wzulfikar/eloquent-simple-ledger)[ Packagist](https://packagist.org/packages/wzulfikar/eloquent-simple-ledger)[ RSS](/packages/wzulfikar-eloquent-simple-ledger/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (7)Used By (0)

Simple Ledger Mechanism via Laravel Eloquent
============================================

[](#simple-ledger-mechanism-via-laravel-eloquent)

### Installation

[](#installation)

`composer require wzulfikar\eloquent-simple-ledger`

### Usage

[](#usage)

```
// find account from table `accounts`
$account = \Wzulfikar\EloquentSimpleLedger\Account::findOrFail($your_account_id);

// assuming that current balance is 0
// and now we want to debit 500
// this will create new debit record in table `account_ledgers`
$account->debit(500, 'initial deposit');

// the balance now is 500
// which is previous balance (0) + current transaction (500)
echo $account->balance;

// let's withdraw some money.
// this will create new credit record in table `account_ledgers`
$account->credit(50, 'withdrawal');

// the balance now is 450
// which is previous balance (500) + current transaction (-50)
echo $account->balance;

```

### Get account balance

[](#get-account-balance)

```
$account = \Wzulfikar\EloquentSimpleLedger\Account::findOrFail($your_account_id);
$account->balance;

```

### Get Previous Balance

[](#get-previous-balance)

```
$account->prev_balance;

```

This method used to calculate the balance after each transaction.

### Record New Debit

[](#record-new-debit)

```
$account->debit($amount, $description);

```

### Record New Credit

[](#record-new-credit)

```
$account->credit($amount, $description);

```

### Get Ledger Records

[](#get-ledger-records)

```
$account->ledger->all();

```

### Behind The Scene

[](#behind-the-scene)

When a transaction happens, eloquent will create new row in `account_ledgers` table. If the transaction is debit, the `debit` column won't be null but the `credit` column will be, and vice versa. Then, it will get value of `balance` column from previous row (where the `account_id` is same as the `account_id` of new row) and add amount of transaction to get the balance of current transaction.

After the transaction finished, eloquent will cache the last balance of `account_ledgers` table in `accounts` table.

### Table Definitions

[](#table-definitions)

Table `accounts` :

- (int) id
    id of account
- (int) balance
    current balance of account

Table `account_ledgers` :

- (int) id
- (int) account\_id
- (int) debit
    amount of debit, nullable.
- (int) credit
    amount of credit, nullable.
- (text) desc
    description of transaction, nullable.
- (int) balance
    sum of previous balance and current transaction. this column ensures we have balance of a transaction of any time.

Table `account_ledgers` contains transactions from all accounts in table `accounts`. Both tables have `balance` column, but with different usage.

`balance` in `account_ledgers` records new balance after every transaction.

*Example Debit* :
if balance of last row is 50 and current row's debit is 10, then the balance of current row will be 50 + 10 = 60.

*Example Credit* :
if balance of last row is 60 and current row's credit is 20, then the balance of current row will be 60 + -(20) = 40.

Since balance of every transaction is recorded in `account_ledgers`, generating report (eg. bank-statement) will be easier.

The `balance` column in table `accounts` used to get balance of particular account, which is actually value of `balance` of last row in table `account_ledgers` of that particular account. This is to avoid querying whole rows in `account_ledgers` just to get balance of an account.

All rows in table `account_ledgers` are meant to be read-only data. A row can only be created, update or modification should not be processed.

> debit, credit &amp; balance are all integers by default.

### Integrating with User Model

[](#integrating-with-user-model)

Say, you want to get balance of currently logged in user, which might be something like this:

```
auth()->user()->account->balance;

```

you need to connect your user model with account model.

*First, make sure the column `account_id` is available in table of your user model and it contains id of account that belongs to the user.*

*Secondly, add eloquent relationship in your user model:*

```
// user model
public function account(){
	return $this->hasOne(Wzulfikar\EloquentSimpleLedger\Account::class);
}

```

### Migration

[](#migration)

Copy files in package's migrations folder into your laravel's migrations folder and run `php artisan migrate`.

If you don't want to copy thus files into your app's migration folder, pass the path to package's migration files in artisan migrate command. Like this: `php artisan migrate --path=vendor/wzulfikar/eloquent-simple-ledger/migrations`

### How it Looks

[](#how-it-looks)

To see how it looks,

- include package's `routes.php` into your app's `routes.php` :
    `require_once base_path('vendor/wzulfikar/eloquent-simple-ledger/routes.php');`
- create dummy data for `account` and `account_ledgers` and then visit `/ledger/{account_id}`.

### Sample View : No Data

[](#sample-view--no-data)

[![image](view-without-data.png)](view-without-data.png)

### Sample View : With Data

[](#sample-view--with-data)

[![image](view-with-data.png)](view-with-data.png)

The sample view included some features:

- export to excel, csv &amp; pdf
- reloading data using ajax
- indicator for latest transaction
- human friendly time using moment.js
- responvie table, sortable columns &amp; searchable -- yes, it uses datatables :)
- debts indicator : balance will be red if it less than 0

### Additional Panels

[](#additional-panels)

- Transaction History

[![image](panel-transaction-history.png)](panel-transaction-history.png)

###  Health Score

37

—

LowBetter than 83% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity32

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~6 days

Total

5

Last Release

3658d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/0616203903ec58d1fbeddceb7b00f1fe75cc71754956b67e9dc39b6ebee783aa?d=identicon)[wzulfikar](/maintainers/wzulfikar)

---

Top Contributors

[![wzulfikar](https://avatars.githubusercontent.com/u/7823011?v=4)](https://github.com/wzulfikar "wzulfikar (37 commits)")

### Embed Badge

![Health badge](/badges/wzulfikar-eloquent-simple-ledger/health.svg)

```
[![Health](https://phpackages.com/badges/wzulfikar-eloquent-simple-ledger/health.svg)](https://phpackages.com/packages/wzulfikar-eloquent-simple-ledger)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
