Download the PHP package laratusk/cashflow without Composer
On this page you can find all versions of the php package laratusk/cashflow. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download laratusk/cashflow
More information about laratusk/cashflow
Files in laratusk/cashflow
Package cashflow
Short Description Double-entry balance transaction ledger for Laravel
License MIT
Informations about the package cashflow
Cashflow
A double-entry balance transaction ledger for Laravel. Track credits, debits, fees, and derived amounts with type-safe balance items, validation, and dependency enforcement.
Installation
Publish the migration and run it:
Optionally publish the config (only needed if you want to override the BalanceTransaction model):
Quick Start
Setup
Add HasBalanceTransactions to any model that holds a balance:
This adds a balanceTransactions() morph-many relationship.
Why Balance Items Are Not Shipped
Cashflow does not ship any BalanceItem classes. Every business has different transaction types -- a SaaS platform has
subscriptions and usage fees, a marketplace has commissions and payouts, a payment processor has gateway fees and
dispute charges.
Balance items are your domain logic. The package provides the base class (BalanceItem), attributes (#[Unique],
#[Rule], #[Requires]), and the Balance engine. You define the items that match your business.
Generating Balance Items
Use the artisan command to scaffold new items interactively:
The command asks:
- Class name -- e.g.
Payment,GatewayFee,Refund - Direction -- Debit or Credit
- Amount source -- Passed from outside, fixed value, or derived from another item
- Unique? -- Whether to add
#[Unique] - Requires? -- Dependency on another balance item
Files are created in app/Cashflow/BalanceItems/. Add validation rules directly in the generated class.
Defining Balance Items
Extend BalanceItem and define direction(). If the constructor has an $amount parameter, it is resolved
automatically -- no need to override amount():
The base class auto-resolves $amount and $direction from constructor properties. Override amount() only when you
need custom logic (derived amounts, fixed values).
Fixed vs Derived Amounts
Items can receive their amount directly or calculate it from siblings:
Implementation with required_without validation:
Override Protection
- Lock a value -- hardcode it in the method, not the constructor.
- Allow override -- accept it as a constructor parameter.
Multi-Currency Items
Override currency() and provide an exchange rate:
When an item's currency differs from the balance currency, an exchange rate other than 1.0 is required -- otherwise
save() throws MissingExchangeRateException.
Attributes
#[Rule(...)]
Laravel validation rules for constructor parameters. Validated on insert(). Supports all Laravel rules including
cross-field rules like required_without.
#[Unique]
One instance per reference morph. Inserting a duplicate throws DuplicateBalanceItemException. Checked against
in-memory and DB items.
#[Requires(ClassName::class)]
Enforces dependency on another balance item. Repeatable. Checked against in-memory and DB items.
Metadata
Attach free-form data to any balance item. Stored as JSON, restored when loading from DB.
Reading metadata back:
Batch Operations
Every save() groups items under a single batch_id (UUID).
Reading from DB
Items loaded from DB are read-only BalanceItem instances with saved = true.
Uniqueness Scoping
Uniqueness is scoped per reference morph:
Real-World Examples
Stripe Payment
TrustPayment (Rate-Based)
Refund
Dispute with Evidence Fee
API Reference
Balance
| Method | Description |
|---|---|
Balance::for(Model $balanceable) |
Create a new balance instance |
->reference(Model $ref) |
Scope to a reference morph |
->currency(string $currency) |
Set the balance currency (ISO 4217) |
->insert(BalanceItem $item) |
Add an item to the batch |
->save() |
Persist unsaved items in a DB transaction |
->batchId() |
Get the batch UUID |
->get() |
Load saved items from DB |
->items() |
All items (saved + unsaved) |
->saved() |
Only persisted items |
->unsaved() |
Only pending items |
->has(string $class) |
Check if an item key exists |
->amountOf(string $class) |
Get amount of a specific item |
Balance::dropBatch(string $id) |
Delete all transactions in a batch |
BalanceItem
| Method | Default | Override |
|---|---|---|
key() |
FQCN | Optional |
amount() |
Auto-resolved from $amount property |
Override for derived/fixed amounts |
direction() |
Auto-resolved from $direction property |
Override to hardcode direction |
currency() |
null (uses balance currency) |
Optional |
exchangeRate() |
1.0 |
Optional |
withMetadata(?array $data) |
Fluent setter | -- |
Exceptions
| Exception | When |
|---|---|
MissingCurrencyException |
No currency on balance or item |
MissingExchangeRateException |
Different currency but rate is 1.0 |
DuplicateBalanceItemException |
#[Unique] item already exists |
MissingRequiredBalanceItemException |
#[Requires] dependency missing |
BatchAlreadySavedException |
save() or insert() after commit |
EmptyBatchException |
save() with no unsaved items |
BalanceItemNotFoundException |
amountOf() for missing key |
BalanceNotSetException |
balance() called before insert() |
Currency
Cashflow uses plain ISO 4217 strings ('USD', 'EUR', 'TRY') for currency codes. No currency enum is shipped -- use
whichever currency library fits your project.
For validation, squirephp/currencies-en works well:
Database Schema
| Column | Type | Notes |
|---|---|---|
| id | bigint (PK) | auto-increment |
| batch_id | uuid | indexed, groups items |
| balanceable_id | bigint | morph FK |
| balanceable_type | string | morph type |
| reference_id | bigint/null | morph FK |
| reference_type | string/null | morph type |
| direction | string | debit / credit |
| amount | bigint unsigned | minor units (cents) |
| currency | string(3) | ISO 4217 |
| exchange_rate | decimal(12,6) | default 1.0 |
| key | string | FQCN by default |
| metadata | json/null | free-form data |
| created_at | timestamp | auto-set |
Configuration
Override the model to customize the table name, casts, or add behavior.
License
MIT
All versions of cashflow with dependencies
illuminate/database Version ^11.0|^12.0|^13.0
illuminate/support Version ^11.0|^12.0|^13.0
illuminate/validation Version ^11.0|^12.0|^13.0