Overview
TheAccountController 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
| Method | URI | Name | Middleware |
|---|---|---|---|
| GET | /accounts | accounts.index | auth, verified |
| GET | /accounts/create | accounts.create | auth, verified |
| POST | /accounts | accounts.store | auth, verified |
| GET | /accounts/{account} | accounts.show | auth, verified, can:view,account |
| GET | /accounts/{account}/edit | accounts.edit | auth, verified, can:update,account |
| PATCH | /accounts/{account} | accounts.update | auth, verified, can:update,account |
| GET | /accounts/{account}/transfer | accounts.transfer | auth, verified, can:update,account |
| PATCH | /accounts/{account}/transfer | accounts.storeTransfer | auth, verified, can:update,account |
| DELETE | /accounts/{account} | accounts.destroy | auth, verified, can:delete,account |
Properties
Methods
index()
Displays a list of all user accounts with their transactions.Returns
Returns theaccounts.index view with:
accounts- Collection of user accounts with transactions loaded
Example Request
Example Response
create()
Shows the form for creating a new account.Returns
Returns theaccounts.create view with:
availableColors- Array of available color optionsselectedColor- Default color selection
Example Request
store()
Stores a newly created account and creates a balance correction transaction if needed.Account name
Initial account balance
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
Example Response
show()
Displays details of a specific account. Supports HTMX requests.The account model instance to display
Returns
- For HTMX requests: Returns an HTMX fragment targeting the
panelelement - For regular requests: Returns the
accounts.showview
Example Request
edit()
Shows the form for editing an account. Supports HTMX requests.The account model instance to edit
Returns
- For HTMX requests: Returns an HTMX fragment targeting the
formelement - For regular requests: Returns the
accounts.editview
account- The account being editedavailableColors- Array of color optionsselectedColor- Current account color
Example Request
update()
Updates an account and creates a balance correction transaction if balance changed.The account model instance to update
Updated account name
Updated account balance
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
Example Response (HTMX)
destroy()
Deletes an account and updates net worth.The account model instance to delete
Behavior
- Deletes the account
- Recalculates user’s net worth
- Returns empty response (for HTMX)
Example Request
transfer()
Shows the form for transferring money between accounts.The source account for the transfer
Returns
- For HTMX requests: Returns an HTMX fragment targeting the
formelement - For regular requests: Returns the
accounts.transferview
account- The source accountuserAccounts- Other user accounts (excluding source account)
Example Request
storeTransfer()
Processes a transfer between two accounts.The source account for the transfer
ID of the destination account
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
Example Response
storeTransferTransactions()
Creates the pair of transactions for an account transfer.The transfer category collection
Destination account
Transfer amount
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 validationApp\Models\Account- Account modelIlluminate\Support\Facades\Auth- Authentication facadeIlluminate\Support\Facades\Validator- Validator facadeMauricius\LaravelHtmx\Http\HtmxRequest- HTMX request handlerMauricius\LaravelHtmx\Facades\HtmxResponse- HTMX response builder
Helper Functions
updateNetworth()- Recalculates and updates user’s net worthflashToast($type, $message)- Displays toast notification