1. Go to this page and download the library: Download syriable/laravel-ledger library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
syriable / laravel-ledger example snippets
use Syriable\Ledger\Facades\Ledger;
Ledger::createLedger(slug: 'platform-main', currency: 'USD');
$result = Ledger::post(new OrderPaidPosting($order));
$result->transaction; // the recorded Transaction
$result->wasReplayed; // true if this reference was already posted
use Syriable\Ledger\Data\EntryDraft;
use Syriable\Ledger\Models\Account;
use Syriable\Ledger\Postings\Posting;
use Syriable\Ledger\ValueObjects\Money;
use Syriable\Ledger\ValueObjects\Reference;
final class OrderPaidPosting extends Posting
{
/**
* Accounts and amounts are resolved by the caller and passed in.
* A Posting must never query the database inside entries() — that
* would break determinism on retry. See docs/04-the-posting-contract.md.
*/
public function __construct(
private readonly string $orderId,
private readonly Account $cash,
private readonly Account $revenue,
private readonly Money $total,
) {}
public function ledger(): string { return 'platform-main'; }
public function currency(): string { return $this->total->currency; }
public function reference(): Reference { return Reference::for('order.paid', $this->orderId); }
public function entries(): array
{
return [
EntryDraft::debit($this->cash, $this->total),
EntryDraft::credit($this->revenue, $this->total),
];
}
}
$cash = Ledger::for('platform-main')->account('platform.cash.usd');
$cash->balance(); // int — signed balance (negative = overdraft)
$cash->balanceMoney(); // Money
$cash->balanceAsOf($moment); // int — historical balance, from entries
$cash->entries; // immutable history
$accounts = Account::query()
->withBalance()
->where('ledger_id', $ledgerId)
->get();
$accounts->each(fn (Account $a) => $a->balance()); // zero extra queries
use Syriable\Ledger\HasAccounts;
class User extends Model
{
use HasAccounts;
public function defaultLedgerSlug(): string
{
return 'platform-main';
}
}
$user->openAccount('available.usd', AccountType::Liability, 'USD');
$user->account('available.usd')->balance();