F
Filament16mo ago
jwktje

Hide a resource widget based on the resource status

Hey everyone. I found the option to hide a widget on a resource with this function: public static function canView(): bool The issue is that I want to only show the widget if the resource record/model is in a certain status. But I can't access the record because the function is static. Does anyone know a workaround? What I would like to do but doesn't work:
class Availability extends Widget
{
public ?Model $record = null;

public static function canView(): bool
{
return $this->record->status === StatusEnum::Request;
}
}
class Availability extends Widget
{
public ?Model $record = null;

public static function canView(): bool
{
return $this->record->status === StatusEnum::Request;
}
}
Something that feels hacky that works:
$record = Event::find(Route::current()->parameter('record'));
return $record->status === StatusEnum::Request;
$record = Event::find(Route::current()->parameter('record'));
return $record->status === StatusEnum::Request;
5 Replies
Luukd_2000
Luukd_200010mo ago
Did you find a better sollution for this?
Bonux
Bonux10mo ago
class FooWidget extends BaseWidget {
use InteractsWithPageTable;

protected function canView(): bool
{
return $this->getPageTableQuery()->where('status', '=', StatusEnum::Request;)->exists();
}
}
class FooWidget extends BaseWidget {
use InteractsWithPageTable;

protected function canView(): bool
{
return $this->getPageTableQuery()->where('status', '=', StatusEnum::Request;)->exists();
}
}
Could this work ? There is an InteractsWithPageTable trait which you can use For the widget if you add that and add a use ExposesTableToWidgets; trait to the resource table you should get the same query executed passed to the widget Then you can try to work with that query and do stuff
Luukd_2000
Luukd_200010mo ago
the problem is that your not in a object context. so you cant use $this
Bonux
Bonux10mo ago
oh yea
Luukd_2000
Luukd_200010mo ago
I fixed it like this protected function getHeaderWidgets(): array { $widgets = []; // Access the current record using $this->record $website = $this->record; // Check if the relationship count is 0 if ($website && $website->websiteChecks()->count() === 0) { $widgets[] = WebsiteResource\Widgets\WebsiteConnection::class; } return $widgets; } in the edit page, you can use conditions when building the array
Want results from more Discord servers?
Add your server