Skip to main content

Overview

The Category model represents transaction categories in Cashify. Categories help organize transactions into types (income, expense, correction, transfer) with customizable colors and icons for visual identification.

Properties

id
integer
required
Unique identifier for the category
user_id
integer
required
Foreign key referencing the user who owns this category
name
string
required
Name of the category (e.g., “Groceries”, “Salary”, “Utilities”)
type
enum
required
Type of the categoryAllowed values:
  • income - For income transactions
  • expense - For expense transactions
  • correction - For balance corrections
  • transfer - For transfers between accounts
color
string
required
Color identifier for visual representationDefault: "gray"
icon
string
required
Icon identifier for visual representationDefault: "image"
created_at
timestamp
When the category was created
updated_at
timestamp
When the category was last updated

Fillable Attributes

The following attributes can be mass-assigned:
  • name
  • type
  • color
  • icon

Public Properties

$shade

The color shade value used for Tailwind CSS classes. Type: integer Default: 200
$category->shade; // 200

Relationships

user()

Returns the user who owns this category. Type: BelongsTo Returns: Illuminate\Database\Eloquent\Relations\BelongsTo
$category->user; // User model

transactions()

Returns all transactions associated with this category. Type: HasMany Returns: Illuminate\Database\Eloquent\Relations\HasMany
$category->transactions; // Collection of Transaction models

Color Methods

The Category model uses the HasColor trait, which provides color management functionality.

getColorClassAttribute()

Returns the full Tailwind CSS class for the category’s color. Returns: string Shade: Uses the model’s $shade property (200)
$category->color_class; // Returns: "bg-green-200"

setColorAttribute()

Sets the color attribute for the category. Parameters: string $color
$category->color = 'green';

getColorAttribute()

Gets the color attribute value. Returns: string
$color = $category->color; // "gray"

getAvailableColors()

Returns an array of all available colors. Returns: array Static method
$colors = Category::getAvailableColors();
// ['gray', 'orange', 'amber', 'yellow', 'lime', 'green', 'emerald', 
//  'teal', 'cyan', 'sky', 'blue', 'indigo', 'violet', 'purple', 
//  'fuchsia', 'pink', 'rose']

getDefaultColor()

Returns the default color value. Returns: string Static method
$defaultColor = Category::getDefaultColor(); // "gray"

Database Indexes

The categories table has the following indexes for optimized queries:
  • name - For searching categories by name
  • type - For filtering by category type
  • user_id - For filtering by user
  • user_id, type - Composite index for user-type queries

Cascade Deletion

Categories are automatically deleted when their associated user is deleted (cascade on delete).

Traits

  • HasFactory - Enables model factories for testing
  • HasColor - Provides color management functionality with Tailwind CSS integration

Example Usage

use App\Models\Category;

// Create a new expense category
$category = Category::create([
    'user_id' => 1,
    'name' => 'Groceries',
    'type' => 'expense',
    'color' => 'green',
    'icon' => 'shopping-cart',
]);

// Access the category's color class
$colorClass = $category->color_class; // "bg-green-200"

// Get all transactions for this category
$transactions = $category->transactions;

// Query categories by type
$expenseCategories = Category::where('type', 'expense')
    ->where('user_id', 1)
    ->orderBy('name')
    ->get();

// Get income categories
$incomeCategories = Category::where('user_id', 1)
    ->where('type', 'income')
    ->get();

// Get available colors for UI
$availableColors = Category::getAvailableColors();

// Count transactions per category
$categoryWithCount = Category::withCount('transactions')
    ->where('user_id', 1)
    ->get();

Build docs developers (and LLMs) love