Rendering widgets on multiple dashboard
Hi everyone,
I'm working on a Filament project with two separate dashboard pages under the same admin panel:
/admin/dashboard
for general dashboard widgets
/admin/statistics
for statistics-related widgets
I want to conditionally render widgets on these pages using the canView() method.
My logic checks the current URL and displays the widgets only on their respective pages.
Here’s how I handle it:
public static function canView(): bool
{
if (request()->is('admin/dashboard')) {
return true;
}
return false;
}
However, I'm running into a problem with the URL when applying filters. For example, when filtering on /admin/statistics, the URL becomes something like this:
http://127.0.0.1:8000/admin/statistics?filters[period]=today&filters[startDate]=2024-10-24
In this case, the canView() logic behaves unexpectedly, and widgets for the statistics page get hidden while dashboard widgets show up, likely because of the filters in the URL.
What I've Tried
I modified the canView() method to check for specific URLs using request()->url() and request()->is() but the issue persists when filters are applied.
I also tried using variables inside the widget and passing them to canView(), but I ran into the issue of using non-static variables inside a static method.
is there a way to render the widgets directly from the page itself like Dashboard or Statistics
protected function getHeaderWidgets(): array
{
return [
GlobalStatistics::class,
// NewUsersTable::class,
CompanyRevenuesDoughnutchart::class,
];
}
but it doesn't work
Is there a better way to handle this scenario where widgets are shown/hidden correctly based on both the URL and the applied filters?
Thanks 🌹0 Replies