Overview
TheCategoryController is a resource controller that handles CRUD operations for transaction categories. It supports HTMX for dynamic UI updates and provides customization options including colors, shades, and icons.
Namespace: App\Http\Controllers
Extends: Controller
Routes
| Method | URI | Name | Middleware |
|---|---|---|---|
| GET | /categories | categories.index | auth, verified |
| GET | /categories/create | categories.create | auth, verified |
| POST | /categories | categories.store | auth, verified |
| GET | /categories/{category} | categories.show | auth, verified, can:view,category |
| GET | /categories/{category}/edit | categories.edit | auth, verified, can:update,category |
| PATCH | /categories/{category} | categories.update | auth, verified, can:update,category |
| DELETE | /categories/{category} | categories.destroy | auth, verified, can:delete,category |
Properties
Methods
index()
Displays categories grouped by type (income and expense).Returns
Returns thecategories.index view with:
incomeCategories- Collection of income categories with transaction countsexpenseCategories- Collection of expense categories with transaction counts
Example Request
Example Response
create()
Shows the form for creating a new category.Returns
Returns thecategories.create view with:
availableColors- Array of available color optionsselectedColor- Default color selectioncolorShade- Default shade value (300)icons- Collection of available icon files frompublic/images/categoriesselectedIcon- Default icon name (“image”)
Example Request
store()
Stores a newly created category.Category name
Category type:
income or expenseColor identifier (e.g., “emerald”, “blue”, “red”)
Color shade value (e.g., “300”, “500”)
Icon filename without extension
Behavior
- Creates the category with validated attributes
- Redirects to categories index
- If type is “income”, adds
tab=2query parameter to show income tab - Displays success toast message
Example Request
Example Response
show()
Displays details of a specific category. Supports HTMX requests.The category model instance to display
Returns
- For HTMX requests: Returns an HTMX fragment targeting the
panelelement - For regular requests: Returns the
categories.showview
Example Request
edit()
Shows the form for editing a category. Supports HTMX requests.The category model instance to edit
Returns
- For HTMX requests: Returns an HTMX fragment targeting the
formelement - For regular requests: Returns the
categories.editview
category- The category being editedavailableColors- Array of color optionsselectedColor- Current category colorcolorShade- Current shade valueicons- Available icon filesselectedIcon- Current category icon
Example Request
update()
Updates an existing category with special handling for type changes.The category model instance to update
Updated category name
Updated category type:
income or expenseUpdated color identifier
Updated shade value
Updated icon filename
Behavior
- Updates the category with validated attributes
- If category type changed:
- Performs a full page redirect to appropriate tab
- Retargets the HTMX response to the new type’s list
- Inserts at the beginning of the list
- If type unchanged:
- Performs in-place update via HTMX
- Replaces the existing element
Example Request
Example Response (HTMX - Type Unchanged)
Example Response (HTMX - Type Changed)
destroy()
Deletes a category.The category model instance to delete
Behavior
- Deletes the category
- Returns empty response (for HTMX)
Example Request
Category Types
The system supports the following category types:income- For income transactions (salary, freelance, etc.)expense- For expense transactions (groceries, rent, etc.)correction- System-generated categories for balance correctionstransfer- System-generated categories for account transfers
The
correction and transfer types are typically created automatically by the system and used internally for balance adjustments and account transfers.Icon Management
Category icons are stored as image files in thepublic/images/categories directory. The controller reads available icons dynamically from this directory.
Supported Icon Operations:
- Icons are displayed by filename (without extension)
- Available icons are loaded from the filesystem
- Custom icons can be added by placing files in the categories directory
Dependencies
App\Http\Requests\CategoryRequest- Form request validationApp\Models\Category- Category modelIlluminate\Support\Facades\Auth- Authentication facadeIlluminate\Support\Facades\File- File system operationsMauricius\LaravelHtmx\Http\HtmxRequest- HTMX request handlerMauricius\LaravelHtmx\Facades\HtmxResponse- HTMX response builder
Helper Functions
flashToast($type, $message)- Displays toast notification
Authorization
All routes require authentication and email verification. View, edit, update, and delete operations are additionally protected by Laravel policy gates that check ownership.HTMX Integration
This controller heavily utilizes HTMX for dynamic UI updates:- Fragment Responses: Returns partial HTML fragments instead of full pages
- Dynamic Retargeting: Changes target elements based on context
- Smart Reswapping: Uses different swap strategies (outerHTML, afterbegin) based on operation
- URL Management: Pushes URLs to browser history for proper navigation