# Database Models and Relationships

**Date:** 2026-08-09

## 1. TradingAccount Model

The `TradingAccount` model has been successfully created to store user trading accounts (e.g. "Tech Stocks", "Play Portfolio"). 
It supports both "play" and "real" modes for simulated vs actual trading.

**Database Schema (`trading_accounts` table):**
- `id`: Primary key
- `user_id`: Foreign key referencing `users.id` (constrained, cascade on delete)
- `name`: String (Name of the portfolio)
- `mode`: String (either 'play' or 'real', defaults to 'play')
- `timestamps`: `created_at` and `updated_at`

**Model Features:**
- Uses Laravel 13 `#[Fillable(['user_id', 'name', 'mode'])]` attribute.
- Belongs to `User` (`user()` relationship).
- Has many `Trade`s (`trades()` relationship).

## 2. Trade Model

The `Trade` model represents individual buy/sell actions associated with a particular trading account.

**Database Schema (`trades` table):**
- `id`: Primary key
- `trading_account_id`: Foreign key referencing `trading_accounts.id` (constrained, cascade on delete)
- `ticker`: String (e.g. "AAPL", "BHP.AX")
- `type`: String ('buy' or 'sell')
- `quantity`: Decimal (8, 4) supporting fractional shares
- `price`: Decimal (10, 4) for high precision share pricing
- `fees`: Decimal (8, 2) for any transactional fees, default 0
- `trade_date`: DateTime of the actual trade execution
- `timestamps`: `created_at` and `updated_at`

**Model Features:**
- Uses Laravel 13 `#[Fillable(['trading_account_id', 'ticker', 'type', 'quantity', 'price', 'fees', 'trade_date'])]` attribute.
- Includes casts for decimals and `trade_date`.
- Belongs to `TradingAccount` (`tradingAccount()` relationship).

## 3. User Model

The `User` model was updated to include the `tradingAccounts()` relationship which is a `HasMany` pointing to the user's trading accounts.
