#canView widget

16 messages · Page 1 of 1 (latest)

stray bluff
#

Hi all how can i use public static function canView() in widget class, i want to display chart only for users with any role ...

crude spokeBOT
#

To help others find answers, you can mark your question as solved via Right click solution message -> Apps -> ✅ Mark Solution

hidden lava
#

By adding it to the widget 🙂

stray bluff
#

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

hidden lava
#

Look at the widget class... it shows what it should be:

    public static function canView(): bool
    {
        return parent::canView(); // TODO: Change the autogenerated stub
    }
stray bluff
#

thanks good friend, it working.

sleek dagger
#

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?

quaint star
plain trail
sleek dagger
#

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().

plain trail
twin spruce
#

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 😦

whole sequoia
# twin spruce I just stumbled into this too - I want to display a widget based on whether some...

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 [];
        }
    }