Skip to main content

Overview

The AccountController is a resource controller that handles CRUD operations for financial accounts and account-to-account transfers. It supports HTMX for dynamic UI updates and automatically manages balance corrections. Namespace: App\Http\Controllers Extends: Controller

Routes

MethodURINameMiddleware
GET/accountsaccounts.indexauth, verified
GET/accounts/createaccounts.createauth, verified
POST/accountsaccounts.storeauth, verified
GET/accounts/{account}accounts.showauth, verified, can:view,account
GET/accounts/{account}/editaccounts.editauth, verified, can:update,account
PATCH/accounts/{account}accounts.updateauth, verified, can:update,account
GET/accounts/{account}/transferaccounts.transferauth, verified, can:update,account
PATCH/accounts/{account}/transferaccounts.storeTransferauth, verified, can:update,account
DELETE/accounts/{account}accounts.destroyauth, verified, can:delete,account

Properties

protected array $availableColors;
protected string $selectedColor;
Initialized in constructor with default account color options.

Methods

index()

Displays a list of all user accounts with their transactions.
public function index(): View

Returns

Returns the accounts.index view with:
  • accounts - Collection of user accounts with transactions loaded

Example Request

curl -X GET https://your-app.com/accounts \
  -H "Authorization: Bearer {token}" \
  -H "Accept: application/json"

Example Response

{
  "accounts": [
    {
      "id": 1,
      "name": "Checking Account",
      "balance": 2500.00,
      "color": "blue",
      "created_at": "2024-01-15T10:30:00Z",
      "transactions_count": 45
    },
    {
      "id": 2,
      "name": "Savings Account",
      "balance": 10000.00,
      "color": "green",
      "created_at": "2024-01-16T14:20:00Z",
      "transactions_count": 12
    }
  ]
}

create()

Shows the form for creating a new account.
public function create(): View

Returns

Returns the accounts.create view with:
  • availableColors - Array of available color options
  • selectedColor - Default color selection

Example Request

curl -X GET https://your-app.com/accounts/create \
  -H "Authorization: Bearer {token}" \
  -H "Accept: application/json"

store()

Stores a newly created account and creates a balance correction transaction if needed.
public function store(AccountRequest $request): RedirectResponse
name
string
required
Account name
balance
float
required
Initial account balance
color
string
required
Account color for UI display

Behavior

  • Creates the account with validated attributes
  • If initial balance is non-zero, creates a “Balance Correction” transaction
  • Updates user’s net worth
  • Displays success toast message

Example Request

curl -X POST https://your-app.com/accounts \
  -H "Authorization: Bearer {token}" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "New Savings Account",
    "balance": 5000.00,
    "color": "emerald"
  }'

Example Response

{
  "success": true,
  "message": "Account created successfully.",
  "redirect": "/accounts"
}

show()

Displays details of a specific account. Supports HTMX requests.
public function show(HtmxRequest $request, Account $account)
account
Account
required
The account model instance to display

Returns

  • For HTMX requests: Returns an HTMX fragment targeting the panel element
  • For regular requests: Returns the accounts.show view

Example Request

curl -X GET https://your-app.com/accounts/1 \
  -H "Authorization: Bearer {token}" \
  -H "Accept: application/json"

edit()

Shows the form for editing an account. Supports HTMX requests.
public function edit(HtmxRequest $request, Account $account)
account
Account
required
The account model instance to edit

Returns

  • For HTMX requests: Returns an HTMX fragment targeting the form element
  • For regular requests: Returns the accounts.edit view
Both responses include:
  • account - The account being edited
  • availableColors - Array of color options
  • selectedColor - Current account color

Example Request

curl -X GET https://your-app.com/accounts/1/edit \
  -H "Authorization: Bearer {token}" \
  -H "HX-Request: true"

update()

Updates an account and creates a balance correction transaction if balance changed.
public function update(AccountRequest $request, Account $account)
account
Account
required
The account model instance to update
name
string
required
Updated account name
balance
float
required
Updated account balance
color
string
required
Updated account color

Behavior

  • If balance changed, creates a “Balance Correction” transaction for the difference
  • Updates the account with new attributes
  • Recalculates user’s net worth
  • Returns HTMX response with updated account panel

Example Request

curl -X PATCH https://your-app.com/accounts/1 \
  -H "Authorization: Bearer {token}" \
  -H "Content-Type: application/json" \
  -H "HX-Request: true" \
  -d '{
    "name": "Updated Checking",
    "balance": 2750.00,
    "color": "blue"
  }'

Example Response (HTMX)

{
  "HX-Push-Url": "/accounts",
  "HX-Retarget": "this",
  "HX-Reswap": "outerHTML",
  "html": "<div id='panel'>...</div>"
}

destroy()

Deletes an account and updates net worth.
public function destroy(Account $account)
account
Account
required
The account model instance to delete

Behavior

  • Deletes the account
  • Recalculates user’s net worth
  • Returns empty response (for HTMX)

Example Request

curl -X DELETE https://your-app.com/accounts/1 \
  -H "Authorization: Bearer {token}" \
  -H "HX-Request: true"

transfer()

Shows the form for transferring money between accounts.
public function transfer(HtmxRequest $request, Account $account)
account
Account
required
The source account for the transfer

Returns

  • For HTMX requests: Returns an HTMX fragment targeting the form element
  • For regular requests: Returns the accounts.transfer view
Both responses include:
  • account - The source account
  • userAccounts - Other user accounts (excluding source account)

Example Request

curl -X GET https://your-app.com/accounts/1/transfer \
  -H "Authorization: Bearer {token}" \
  -H "Accept: application/json"

storeTransfer()

Processes a transfer between two accounts.
public function storeTransfer(HtmxRequest $request, Account $account)
account
Account
required
The source account for the transfer
to_account
int
required
ID of the destination account
amount
float
required
Amount to transfer (minimum 0.01)

Behavior

  • Validates the transfer request
  • Creates two transfer transactions (one outgoing, one incoming)
  • Updates both account balances
  • Uses “transfer” category type for both transactions

Example Request

curl -X PATCH https://your-app.com/accounts/1/transfer \
  -H "Authorization: Bearer {token}" \
  -H "Content-Type: application/json" \
  -d '{
    "to_account": 2,
    "amount": 500.00
  }'

Example Response

{
  "success": true,
  "redirect": "/accounts"
}

storeTransferTransactions()

Creates the pair of transactions for an account transfer.
public function storeTransferTransactions($transferCategory, $toAccount, $amount, $fromAccount): void
transferCategory
Collection
required
The transfer category collection
toAccount
Account
required
Destination account
amount
float
required
Transfer amount
fromAccount
Account
required
Source account

Behavior

  • Creates a positive transaction for the destination account
  • Creates a negative transaction for the source account
  • Both transactions include detailed information about the transfer

Dependencies

  • App\Http\Requests\AccountRequest - Form request validation
  • App\Models\Account - Account model
  • Illuminate\Support\Facades\Auth - Authentication facade
  • Illuminate\Support\Facades\Validator - Validator facade
  • Mauricius\LaravelHtmx\Http\HtmxRequest - HTMX request handler
  • Mauricius\LaravelHtmx\Facades\HtmxResponse - HTMX response builder

Helper Functions

  • updateNetworth() - Recalculates and updates user’s net worth
  • flashToast($type, $message) - Displays toast notification

Authorization

All routes require authentication and email verification. View, edit, update, transfer, and delete operations are additionally protected by Laravel policy gates that check ownership.

Build docs developers (and LLMs) love