Skip to main content

Overview

The DashboardController is an invokable controller that generates the main dashboard view for authenticated users. It aggregates financial data including accounts, net worth, transaction summaries, and chart data for visualization. Namespace: App\Http\Controllers Extends: Controller

Routes

MethodURINameMiddleware
GET/dashboarddashboardauth, verified

Methods

__invoke()

The main invokable method that handles dashboard requests and returns the dashboard view with aggregated financial data.
public function __invoke(NetworthChart $networthChart, SpendingChart $spendingChart)
networthChart
NetworthChart
required
Chart service for generating net worth data visualization
spendingChart
SpendingChart
required
Chart service for generating spending data visualization

Returns

Returns an Illuminate view with the following data:
  • groupedTransactions - Recent transactions grouped by date (last 5)
  • groupedExpenses - Recent expense transactions grouped by date (last 5)
  • groupedIncomes - Recent income transactions grouped by date (last 5)
  • accounts - All user accounts with their transactions
  • netWorth - Latest net worth value
  • dateRanges - Date ranges for the net worth chart
  • networthChartData - Net worth data points for charting
  • spendingChartData - Spending amounts rounded to 2 decimals
  • spendingChartLabels - Labels for the spending chart
  • totalExpenses - Sum of all expense transactions
  • totalIncomes - Sum of all income transactions

Example Request

curl -X GET https://your-app.com/dashboard \
  -H "Authorization: Bearer {token}" \
  -H "Accept: application/json"

Example Response

{
  "view": "dashboard",
  "data": {
    "accounts": [
      {
        "id": 1,
        "name": "Checking Account",
        "balance": 5000.00,
        "color": "blue"
      }
    ],
    "netWorth": 15000.00,
    "totalExpenses": 3500.00,
    "totalIncomes": 8500.00,
    "networthChartData": [10000, 12000, 13000, 15000],
    "dateRanges": ["Jan", "Feb", "Mar", "Apr"]
  }
}

Private Methods

getUserAccounts()

Retrieves all accounts for a specific user with their associated transactions.
private function getUserAccounts($userId)
userId
int
required
The ID of the user
Returns: Collection of Account models with transactions relationship loaded

getLatestNetWorth()

Fetches the most recent net worth value for a user.
private function getLatestNetWorth($userId)
userId
int
required
The ID of the user
Returns: Float value of the latest net worth or null

getTransactionTotals()

Calculates total expenses and incomes for a user by joining with categories.
private function getTransactionTotals($userId)
userId
int
required
The ID of the user
Returns: Object with total_expenses and total_incomes properties

getGroupedTransactions()

Fetches and groups the 5 most recent transactions by type (all, expenses, incomes).
private function getGroupedTransactions($userId): array
userId
int
required
The ID of the user
Returns: Array containing [groupedTransactions, groupedExpenses, groupedIncomes]

fetchAndGroupTransactions()

Fetches transactions optionally filtered by type and groups them by date.
private function fetchAndGroupTransactions($userId, string $type = null): array
userId
int
required
The ID of the user
type
string
Optional transaction type filter (expense or income)
Returns: Array of transactions grouped by date

Dependencies

  • App\Charts\NetworthChart - Service for generating net worth chart data
  • App\Charts\SpendingChart - Service for generating spending chart data
  • App\Models\Account - Account model
  • App\Models\NetWorth - Net worth model
  • App\Models\Transaction - Transaction model
  • Illuminate\Support\Facades\Auth - Authentication facade

Authorization

This controller requires the user to be authenticated and have a verified email address via the auth and verified middleware.

Build docs developers (and LLMs) love