F
Filament8mo 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 ...
11 Replies
toeknee
toeknee8mo ago
By adding it to the widget 🙂
Sydd
SyddOP8mo 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
toeknee8mo 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
SyddOP8mo ago
thanks good friend, it working.
Hugo
Hugo8mo 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
hrank8t8mo 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 Koch8mo ago
$this->record or $this->getRecord() probably?
Hugo
Hugo8mo 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 Koch8mo 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.
Adam Holmes
Adam Holmes5mo ago
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 😦
Jakub
Jakub3mo ago
I got this working like this you access the record there and just pass in the widget or not In the ViewResource page
protected function getHeaderWidgets(): array
{
$feedbackRequestsCount = Task::where('thread_id', $this->record->id)
->whereIn('status', [EStatus::ACTIVE, EStatus::SUBMITTED])
->where('type', ETaskType::QUESTION)
->count();

if ($feedbackRequestsCount > 0) {
return [
ThreadFeedbackRequestsTableWidget::class,
];
} else {
return [];
}
}
protected function getHeaderWidgets(): array
{
$feedbackRequestsCount = Task::where('thread_id', $this->record->id)
->whereIn('status', [EStatus::ACTIVE, EStatus::SUBMITTED])
->where('type', ETaskType::QUESTION)
->count();

if ($feedbackRequestsCount > 0) {
return [
ThreadFeedbackRequestsTableWidget::class,
];
} else {
return [];
}
}
@hrank8t @Hugo if ur still looking for answer
Want results from more Discord servers?
Add your server