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 ...
11 Replies
By adding it to the widget 🙂
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
Look at the widget class... it shows what it should be:
thanks good friend, it working.
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?
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.
$this->record
or $this->getRecord()
probably?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().
Right. It’s a static method. I was misled by the code above. Not sure whether there is a way to check the record.
I just stumbled into this too - I want to display a widget based on whether something is set in the record. I can't find a way to do it as
canView
is static and seems to be called before anything else is instantiated and loaded into the widget 😦I got this working like this you access the record there and just pass in the widget or not
In the ViewResource page
@hrank8t @Hugo if ur still looking for answer