F
Filament2mo ago
Anik

resource in multiple panels

Hi, I am trying to reuse same model resource in different panels and display a slight different array of pages according to user. In the resource getPages() the filament currentpanel and auth()->user() are not available. what am i doing wrong?
public static function getPages(): array
{
dd(Filament::getCurrentPanel());
if (Filament::auth()->user() instanceof Admin) {
return [
'index' => Pages\ListBusinesses::route('/'),
'view' => Pages\ViewProfile::route('/{record}'),
'edit' => Pages\EditProfile::route('/{record}/edit'),
'viewKit' => Pages\ViewBusinessKit::route('/{record}/view-kit'),
'editKit' => Pages\EditBusinessKit::route('/{record}/edit-kit'),
'calender' => Pages\ViewCalender::route('/{record}/calender'),
];
}
}
public static function getPages(): array
{
dd(Filament::getCurrentPanel());
if (Filament::auth()->user() instanceof Admin) {
return [
'index' => Pages\ListBusinesses::route('/'),
'view' => Pages\ViewProfile::route('/{record}'),
'edit' => Pages\EditProfile::route('/{record}/edit'),
'viewKit' => Pages\ViewBusinessKit::route('/{record}/view-kit'),
'editKit' => Pages\EditBusinessKit::route('/{record}/edit-kit'),
'calender' => Pages\ViewCalender::route('/{record}/calender'),
];
}
}
Edit: I have kept the resources seperate and the pages common. Then in the pages, i am passing the correct resource after checking auth. this seems to work. what would be the optimum way? It would be less code if filament() would work inside getPages()
/**
* @return class-string
*/
public static function getResource(): string
{
if (filament()->auth()->user() instanceof Business) {
return LocationBusinessResource::class;
}
return BusinessResource::class;
}
/**
* @return class-string
*/
public static function getResource(): string
{
if (filament()->auth()->user() instanceof Business) {
return LocationBusinessResource::class;
}
return BusinessResource::class;
}
1 Reply
Anik
Anik2mo ago
bump