Skip to main content

Overview

The Account model represents financial accounts (e.g., bank accounts, wallets, credit cards) in Cashify. Each account belongs to a user and tracks a balance with customizable color coding.

Properties

id
integer
required
Unique identifier for the account
user_id
integer
required
Foreign key referencing the user who owns this account
name
string
required
Name of the account (e.g., “Chase Checking”, “Savings Account”)
balance
decimal
required
Current balance of the account with precision of 15 digits and 2 decimal placesDefault: 0.00
color
string
required
Color identifier for visual representationDefault: "gray"
created_at
timestamp
When the account was created
updated_at
timestamp
When the account was last updated

Fillable Attributes

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

Relationships

user()

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

transactions()

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

Color Methods

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

getColorClassAttribute()

Returns the full Tailwind CSS class for the account’s color. Returns: string Default shade: 300
$account->color_class; // Returns: "bg-blue-300"

setColorAttribute()

Sets the color attribute for the account. Parameters: string $color
$account->color = 'blue';

getColorAttribute()

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

getAvailableColors()

Returns an array of all available colors. Returns: array Static method
$colors = Account::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 = Account::getDefaultColor(); // "gray"

Database Indexes

The accounts table has the following indexes for optimized queries:
  • name - For searching accounts by name
  • user_id, balance - Composite index for user-balance queries

Cascade Deletion

Accounts 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\Account;

// Create a new account
$account = Account::create([
    'user_id' => 1,
    'name' => 'Checking Account',
    'balance' => 5000.00,
    'color' => 'blue',
]);

// Access the account's color class
$colorClass = $account->color_class; // "bg-blue-300"

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

// Update account balance
$account->update(['balance' => $account->balance + 100]);

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

// Query accounts by user with minimum balance
$accounts = Account::where('user_id', 1)
    ->where('balance', '>=', 1000)
    ->orderBy('balance', 'desc')
    ->get();

Build docs developers (and LLMs) love