How do I create a custom dashboard lens?
To create a dashboard lens you need to create a class that extends Aero\Admin\AdminLens and implement the data method:
class OrdersLens extends \Aero\Admin\AdminLens
{
protected $title = 'Orders';
protected static $permission = 'reports.orders';
protected static $dateGetVariables = [
'custom' => 'ordered-at-date',
'start' => 'ordered-at-date-start-date',
'end' => 'ordered-at-date-end-date',
];
public function data(\Illuminate\Http\Request $request, array $date, array $compare): \Illuminate\Http\JsonResponse
{
$ordersCount = \Aero\Cart\Models\Order::visible()->whereBetween('ordered_at', [$date['start'], $date['end']])->count();
$ordersCompareCount = \Aero\Cart\Models\Order::visible()->whereBetween('ordered_at', [$compare['date']['start'], $compare['date']['end']])->count();
if ($ordersCompareCount !== 0) {
$percentage = (($ordersCount - $ordersCompareCount) / ($ordersCompareCount > 0 ? $ordersCompareCount : 1)) * 100;
}
return response()->json([[
'text' => number_format($ordersCount),
'compare' => [
'text' => $ordersCompareCount === 0 ? 'No data to compare' : $compare['text'],
'percentage' => isset($percentage) ? ($percentage >= 0 ? '+' : '') . number_format($percentage, 2) . '%' : '',
'up' => isset($percentage) ? $percentage >= 0 : '',
],
]]);
}
protected function link(): ?array
{
return auth()->user()->can('reports.view') ? [
'route' => route('admin.report', ['key' => 'order-breakdown']),
'text' => 'View Report',
] : null;
}
}
Parameters
| Parameter | Type | Description |
$request |
\Illuminate\Http\Request |
The current request. |
$date |
array |
Contains the selected start & end dates. |
$compare |
array |
Contains the compare date (start & end nested instead) and the text. |