Organizing
Dashboards
Build dashboards of widget cards, register them, and set their name, icon, refresh button and access.
A dashboard is a named page of widgets. It gives you a high-level view of the numbers that matter in your app. Lathe ships one dashboard, Main, and you can add as many more as you need.
The Main dashboard
Main is the home page of the panel, at /lathe. It shows as "Dashboard" in the sidebar and has no widgets. lathe:install writes app/Lathe/Dashboards/Main.php for you. Fill in its widgets() method to give the home page some cards:
<?php
namespace App\Lathe\Dashboards;
use App\Lathe\Widgets\NewUsers;
use App\Lathe\Widgets\OrdersPerDay;
use Lathe\Dashboards\Main as Dashboard;
use Lathe\Http\Requests\LatheRequest;
class Main extends Dashboard
{
public function widgets(LatheRequest $request): array
{
return [
NewUsers::make(),
OrdersPerDay::make()->fullWidth(),
];
}
}
Note:
Mainoverridesname()to return "Dashboard". Overridename()again in your class to change it.
Create a dashboard
Run the generator:
php artisan lathe:dashboard UserInsights
It writes app/Lathe/Dashboards/UserInsights.php. A dashboard extends Lathe\Dashboards\Dashboard and returns its widgets from widgets():
<?php
namespace App\Lathe\Dashboards;
use App\Lathe\Widgets\NewUsers;
use App\Lathe\Widgets\UsersByPlan;
use Lathe\Dashboards\Dashboard;
use Lathe\Http\Requests\LatheRequest;
class UserInsights extends Dashboard
{
public function widgets(LatheRequest $request): array
{
return [
NewUsers::make(),
UsersByPlan::make(),
];
}
}
Lathe calls widgets() only when someone opens this dashboard, so a heavy list does not slow the other pages.
Register a dashboard
Register your dashboards in the registerDashboards() method of app/Providers/LatheServiceProvider.php:
use App\Lathe\Dashboards\Main;
use App\Lathe\Dashboards\UserInsights;
use Lathe\Lathe;
protected function registerDashboards(): void
{
Lathe::registerDashboards(Main::class, UserInsights::class);
}
registerDashboards() takes class names or instances. A registered dashboard appears in the sidebar under "Dashboards". If you register no dashboard with the key main, Lathe uses its built-in empty Main.
Two dashboards must not share a key. If they do, Lathe stops with an error that names both classes. Override uriKey() on one of them to fix it.
Name and URL
The name comes from the class name. UserInsights becomes "User Insights". Override name() to change it:
public function name(): string
{
return 'User analytics';
}
The URL key is the kebab-case class name, so UserInsights lives at /lathe/dashboards/user-insights. Override the static uriKey() to change it. The key main is special: that dashboard lives at the panel home.
public static function uriKey(): string
{
return 'user-analytics';
}
Description, icon and refresh button
A description shows under the dashboard title. An icon shows in the sidebar and beside the title. A refresh button reloads every card at once. Set each with a PHP attribute on the class:
use Lathe\Attributes\Dashboard\Description;
use Lathe\Attributes\Dashboard\Icon;
use Lathe\Attributes\Dashboard\ShowRefreshButton;
use Lathe\Dashboards\Dashboard;
use Lathe\Enums\Icon as IconName;
#[Icon(IconName::Users)]
#[Description('Watch user growth and engagement.')]
#[ShowRefreshButton]
class UserInsights extends Dashboard
{
// ...
}
Or use the matching methods when you register the dashboard:
Lathe::registerDashboards(
UserInsights::make()
->icon(IconName::Users)
->description('Watch user growth and engagement.')
->showRefreshButton(),
);
| Attribute | Method | Effect |
|---|---|---|
#[Icon(Icon)] |
icon(Icon $icon) |
The sidebar and title icon |
#[Description(string)] |
description(string $text) |
The line under the title |
#[ShowRefreshButton] |
showRefreshButton() |
A button that reloads every card |
The range
The header holds one range picker, and every card on the dashboard shows the range it picks. It offers 7 days, 30 days and 12 months, opens on 30 days, and keeps the choice in the URL as ?range=365. Override ranges() to offer other day counts:
public function ranges(): array
{
return [7 => '7 days', 90 => '90 days'];
}
A card shows the picked range when its own ranges() has that key. A card without it shows its default range and says which one, such as "Last 30 days" beside a value card's number. A card with no ranges() at all, such as a bar or partition card, gets the picked range as $request->query('range') and decides what it means. Return an empty array to hide the picker.
Each user's layout
Every dashboard is customizable. A user can save their own order, size and hidden cards, and reset to the layout in your code. The layout is stored per user in the lathe_user_preferences table, through PUT and DELETE on api/dashboards/{dashboard}/layout. The PUT body lists the cards in order:
{"widgets": [{"uriKey": "revenue", "width": "1/2", "hidden": false}]}
A width is a CardWidth value: 1/4, 1/3, 1/2, 2/3, 3/4 or full. A card you remove from code drops out of a saved layout, and a card you add shows at the end, at its code width. Two saves in a row both succeed, and the last one wins.
Turn it off for a dashboard with customizable(false). The layout routes then answer 403 and any saved layout is ignored:
Lathe::registerDashboards(Finance::make()->customizable(false));
Customize
On the page, a user presses Customize to change their layout. Each card shows a drag handle and a ⋯ menu with five sizes (Small, Medium, Large, Wide and Full) and Hide from this dashboard. A card moves by dragging its handle, or with the keyboard: focus the handle, press Space, move it with the arrow keys and press Space again. The Add a widget panel lists the hidden cards, and Add puts one at the end of the page. Save layout stores the layout for that user, Cancel drops the changes, and Reset to default deletes the saved layout. A dashboard with customizable(false) has no Customize button.
The sidebar entry
Each dashboard adds its own link to the sidebar through its menu() method. Override it to change that link, for example to add a badge:
use Lathe\Http\Requests\LatheRequest;
use Lathe\Menu\MenuItem;
public function menu(LatheRequest $request): MenuItem
{
return parent::menu($request)->badge('New');
}
Menus covers badges and the rest of the sidebar.
Authorization
Hide a dashboard from some users with canSee() or canSeeWhen():
Lathe::registerDashboards(
Main::make(),
UserInsights::make()->canSee(fn ($request) => $request->user()->is_admin),
Finance::make()->canSeeWhen('viewFinance'),
);
canSeeWhen() checks a Gate ability for the signed-in panel user. A dashboard the user may not see leaves the sidebar, and opening its URL by hand is refused.
A user who may not see Main still opens /lathe without an error. Lathe sends them to the first dashboard they may see, else to the first sidebar page they may open. A user who may open nothing gets the 403 page. Signing in, the brand link and starting or stopping an impersonation all land at /lathe, so they follow the same path.
What you see
After you register a dashboard, the sidebar lists it under "Dashboards" with its icon. Opening it shows its title, the optional description line, and a grid of cards that fill in as their numbers arrive. With the refresh button on, each card shows a loading skeleton and then its new value when you click it.