Overview
TheTransactionController is a resource controller that handles CRUD operations for financial transactions. It supports filtering, pagination, and automatically updates account balances and net worth.
Namespace: App\Http\Controllers
Extends: Controller
Routes
| Method | URI | Name | Middleware |
|---|---|---|---|
| GET | /transactions | transactions.index | auth, verified |
| GET | /transactions/create | transactions.create | auth, verified |
| POST | /transactions | transactions.store | auth, verified |
| GET | /transactions/{transaction}/edit | transactions.edit | auth, verified, can:update,transaction |
| PATCH | /transactions/{transaction} | transactions.update | auth, verified, can:update,transaction |
| DELETE | /transactions/{transaction} | transactions.destroy | auth, verified, can:delete,transaction |
Methods
index()
Displays a paginated list of transactions with optional filtering.Filter transactions by category ID
Filter transactions with minimum amount
Filter transactions with maximum amount
Page number for pagination (10 items per page)
Returns
Returns thetransactions.index view with:
transactions- Paginated transactions grouped by datecategories- All user categories for filteringminAmount- Minimum transaction amount in databasemaxAmount- Maximum transaction amount in database
Example Request
Example Response
create()
Shows the form for creating a new transaction.Returns
Returns thetransactions.create view with:
incomeCategories- Collection of income categoriesexpenseCategories- Collection of expense categoriesaccounts- User’s accounts
Example Request
store()
Stores a newly created transaction and updates account balance.Transaction title/description
Transaction amount (positive value, automatically negated for expenses)
ID of the category
ID of the account
Optional transaction details/notes
Optional custom transaction date (defaults to now)
Behavior
- Automatically negates amount for expense transactions
- Updates the associated account balance
- Recalculates user’s net worth
- Displays success toast message
Example Request
Example Response
edit()
Shows the form for editing a transaction.The transaction model instance to edit
Returns
Returns thetransactions.edit view with:
transaction- The transaction being editedcategories- Categories matching the transaction typeaccounts- User’s accountsselectedCategory- Currently selected categoryselectedAccount- Currently selected account
Example Request
update()
Updates an existing transaction and adjusts account balances accordingly.The transaction model instance to update
Updated transaction title
Updated transaction amount
Updated category ID
Updated account ID
Updated transaction details
Behavior
- Handles account changes by updating both old and new account balances
- Automatically negates amount for expense transactions
- Recalculates user’s net worth
- Displays success toast message
Example Request
Example Response
destroy()
Deletes a transaction and creates a correction transaction to maintain balance integrity.The transaction model instance to delete
Behavior
- Creates a “Balance Correction” transaction to reverse the deleted transaction
- Uses the user’s “correction” category type
- Updates account balance
- Recalculates user’s net worth
- Displays success toast message
Example Request
Example Response
Dependencies
App\Filters\TransactionFilter- Filter class for transaction queriesApp\Http\Requests\TransactionRequest- Form request validationApp\Models\Account- Account modelApp\Models\Category- Category modelApp\Models\Transaction- Transaction modelIlluminate\Support\Facades\Auth- Authentication facade
Helper Functions
This controller uses the following helper functions:groupTransactionsByDate($transactions)- Groups transactions by their dateupdateNetworth()- Recalculates and updates user’s net worthflashToast($type, $message)- Displays toast notification