When building a dashboard with Financial information - it's nice to have it updated in real-time. So let's use Laravel Reverb to update a table and a few of our charts.
All the data to the dashboard comes from a DashboardController
Controller.
app/Http/Controllers/DashboardController.php:
use Illuminate\View\View;use App\Services\OrdersService; class DashboardController extends Controller{ public function __invoke(OrdersService $ordersService): View { $totalRevenue = $ordersService->getTotalRevenue(); $thisMonthRevenue = $ordersService->getThisMonthRevenue(); $todayRevenue = $ordersService->getTodayRevenue(); $latestOrders = $ordersService->getLatestOrders(5); $orderChartByMonth = $ordersService->orderChartByMonth(); $orderChartByDay = $ordersService->orderChartByDay(); return view( 'dashboard', compact( 'totalRevenue', 'thisMonthRevenue', 'todayRevenue', 'latestOrders', 'orderChartByDay', 'orderChartByMonth' ) ); }}
Calculations for the data are made using the OrdersService
service.
app/Services/OrdersService.php:
use App\Models\Order;use Illuminate\Database\Eloquent\Collection; class OrdersService{ public function getTotalRevenue(): float|int { return Order::query() ->pluck('total') ->sum() / 100; } // ....
In this example, you will see:
- Service usage to retrieve the data
- Laravel Reverb usage to update the dashboard in real-time