Skip to main content

Overview

The Transaction model represents financial transactions in Cashify. Each transaction belongs to a user, is associated with an account and category, and tracks the amount and details of the financial activity.

Properties

id
integer
required
Unique identifier for the transaction
user_id
integer
required
Foreign key referencing the user who owns this transaction
category_id
integer
required
Foreign key referencing the category of this transaction
account_id
integer
required
Foreign key referencing the account this transaction belongs to
title
string
required
Title or description of the transaction
amount
decimal
required
Transaction amount with precision of 15 digits and 2 decimal placesDefault: 0.00
details
text
Additional details or notes about the transaction
created_at
timestamp
When the transaction was created
updated_at
timestamp
When the transaction was last updated

Mass Assignment

All attributes are mass-assignable (using $guarded = []), which means you can set any field when creating or updating a transaction.
While all fields are mass-assignable, ensure proper validation in your controllers to prevent unauthorized data manipulation.

Relationships

user()

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

account()

Returns the account this transaction is associated with. Type: BelongsTo Returns: Illuminate\Database\Eloquent\Relations\BelongsTo
$transaction->account; // Account model

category()

Returns the category of this transaction. Type: BelongsTo Returns: Illuminate\Database\Eloquent\Relations\BelongsTo
$transaction->category; // Category model

Database Indexes

The transactions table has the following indexes for optimized queries:
  • title - For searching transactions by title
  • user_id - For filtering by user
  • category_id - For filtering by category
  • account_id - For filtering by account
  • user_id, category_id - Composite index for user-category queries
  • user_id, account_id - Composite index for user-account queries

Cascade Deletion

Transactions are automatically deleted when their associated user, category, or account is deleted (cascade on delete).

Traits

  • HasFactory - Enables model factories for testing

Example Usage

use App\Models\Transaction;

// Create a new transaction
$transaction = Transaction::create([
    'user_id' => 1,
    'account_id' => 2,
    'category_id' => 3,
    'title' => 'Grocery Shopping',
    'amount' => 125.50,
    'details' => 'Weekly groceries at supermarket',
]);

// Access related models
$user = $transaction->user;
$account = $transaction->account;
$category = $transaction->category;

// Query transactions by category
$expenseTransactions = Transaction::whereHas('category', function ($query) {
    $query->where('type', 'expense');
})->get();

// Get transactions for a specific account
$accountTransactions = Transaction::where('account_id', 1)
    ->orderBy('created_at', 'desc')
    ->get();

Build docs developers (and LLMs) love