Skip to main content
The NetWorth model stores historical snapshots of a user’s net worth, enabling time-series charting on the dashboard.

Model Overview

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class NetWorth extends Model

Properties

id
integer
Primary key - Auto-incrementing unique identifier
user_id
integer
Foreign key to the users table
net_worth
decimal(15,2)
Snapshot of the user’s net worth at the time of creation. Calculated as sum of all account balances.
created_at
timestamp
Timestamp when this snapshot was created
updated_at
timestamp
Timestamp when this record was last updated

Fillable Attributes

protected $fillable = [
    'user_id',
    'net_worth',
];
Both user_id and net_worth can be mass-assigned.

Relationships

user()

Belongs to a User.
public function user(): BelongsTo
{
    return $this->belongsTo(User::class);
}
Returns: BelongsTo<User> - The user who owns this net worth snapshot Usage:
$netWorth = NetWorth::find(1);
$user = $netWorth->user; // Get the associated user

Database Schema

From migration 2024_06_14_110400_create_net_worths_table.php:
Schema::create('net_worths', function (Blueprint $table) {
    $table->id();
    $table->foreignId('user_id')->constrained()->cascadeOnDelete();
    $table->decimal('net_worth', 15, 2);
    $table->timestamps();
});
Indexes:
  • Primary key on id
  • Foreign key on user_id with cascade delete
Cascade Behavior:
  • Deleting a user automatically deletes all their net worth snapshots

Usage Examples

Creating a Net Worth Snapshot

use App\Models\NetWorth;
use Illuminate\Support\Facades\Auth;

// Calculate current net worth from account balances
$currentNetWorth = Auth::user()
    ->accounts()
    ->sum('balance');

// Create snapshot
NetWorth::create([
    'user_id' => Auth::id(),
    'net_worth' => $currentNetWorth,
]);

Retrieving Latest Net Worth

$latestNetWorth = NetWorth::query()
    ->where('user_id', Auth::id())
    ->latest('created_at')
    ->value('net_worth');

// Returns: 15420.50 (decimal)

Getting Historical Data for Charts

// Get net worth snapshots for the last 30 days
$historicalData = NetWorth::query()
    ->where('user_id', Auth::id())
    ->where('created_at', '>=', now()->subDays(30))
    ->orderBy('created_at')
    ->get(['created_at', 'net_worth']);

// Format for chart
$chartData = $historicalData->map(function ($snapshot) {
    return [
        'date' => $snapshot->created_at->format('Y-m-d'),
        'value' => (float) $snapshot->net_worth,
    ];
});

Net Worth Calculation Logic

Net worth is calculated by summing all account balances:
// In DashboardController or a scheduled task
public function updateNetWorth()
{
    $totalBalance = Account::query()
        ->where('user_id', Auth::id())
        ->sum('balance');

    NetWorth::create([
        'user_id' => Auth::id(),
        'net_worth' => $totalBalance,
    ]);
}
Net worth includes both positive balances (assets) and negative balances (liabilities). A negative account balance reduces the net worth.

Dashboard Integration

The DashboardController uses net worth data for charting:
// From DashboardController
private function getLatestNetWorth($userId)
{
    return NetWorth::query()
        ->where('user_id', $userId)
        ->latest('created_at')
        ->value('net_worth');
}
This value is displayed on the dashboard summary card and used as the latest data point in the net worth chart.

Best Practices

Create net worth snapshots periodically (e.g., daily via scheduled task) or after significant balance changes (large transactions, account transfers).
Consider implementing a retention policy to archive or delete very old snapshots to keep the table manageable. For example, keep daily snapshots for 1 year, then weekly snapshots indefinitely.
For users with many snapshots, add an index on (user_id, created_at) for faster queries:
$table->index(['user_id', 'created_at']);

Build docs developers (and LLMs) love