F
Filament3mo ago
Sydd

canView widget

Hi all how can i use public static function canView() in widget class, i want to display chart only for users with any role ...
9 Replies
toeknee
toeknee3mo ago
By adding it to the widget 🙂
Sydd
Sydd3mo ago
when i try class AmbulanceChart extends ChartWidget { protected function canView(): bool { return false; } Cannot make static method Filament\Widgets\Widget::canView() non static in class App\Filament\Resources\AmbulanceResource\Widgets\AmbulanceChart
toeknee
toeknee3mo ago
Look at the widget class... it shows what it should be:
public static function canView(): bool
{
return parent::canView(); // TODO: Change the autogenerated stub
}
public static function canView(): bool
{
return parent::canView(); // TODO: Change the autogenerated stub
}
Sydd
Sydd3mo ago
thanks good friend, it working.
Hugo
Hugo3mo ago
Hey, it might have nothign related to this but, is there a way I can hide the widget if it has no data? It just shows a blank rectangle when there is no data to show on the chart widget. I tried to do this with canView method and it works when im not doing a query to a specific $record but when my chart has a record, I can't access the $this->record inside the canView method since it says it's not an object. Is there a way to just hide the widget when there's no data to show?
hrank8t
hrank8t3mo ago
I too have the same exact question. How should I access the record in canView() ? If record is null then hide the widget. If anyone knows, please help us out.
Dennis Koch
Dennis Koch3mo ago
$this->record or $this->getRecord() probably?
Hugo
Hugo3mo ago
it says its not an object therefore can't use $this what I got from testing is that canView() is the first thing to be checked as soon as the page tries to show the widget. I've tried to dd on the render and even on the mount and they are only called after the canView() method. This is my thought process, idk if its really like that but it would never go inside the render or mount method before the canView().
Dennis Koch
Dennis Koch3mo ago
Right. It’s a static method. I was misled by the code above. Not sure whether there is a way to check the record.