NetWorth model stores historical snapshots of a user’s net worth, enabling time-series charting on the dashboard.
Model Overview
Properties
Primary key - Auto-incrementing unique identifier
Foreign key to the users table
Snapshot of the user’s net worth at the time of creation. Calculated as sum of all account balances.
Timestamp when this snapshot was created
Timestamp when this record was last updated
Fillable Attributes
user_id and net_worth can be mass-assigned.
Relationships
user()
Belongs to a User.BelongsTo<User> - The user who owns this net worth snapshot
Usage:
Database Schema
From migration2024_06_14_110400_create_net_worths_table.php:
- Primary key on
id - Foreign key on
user_idwith cascade delete
- Deleting a user automatically deletes all their net worth snapshots
Usage Examples
Creating a Net Worth Snapshot
Retrieving Latest Net Worth
Getting Historical Data for Charts
Net Worth Calculation Logic
Net worth is calculated by summing all account balances:Net worth includes both positive balances (assets) and negative balances (liabilities). A negative account balance reduces the net worth.
Dashboard Integration
TheDashboardController uses net worth data for charting:
Best Practices
When to create snapshots
When to create snapshots
Create net worth snapshots periodically (e.g., daily via scheduled task) or after significant balance changes (large transactions, account transfers).
Data retention
Data retention
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.
Performance
Performance
For users with many snapshots, add an index on
(user_id, created_at) for faster queries: