phplaravellaravel-filamentfilamentphp

How do I change the title of a Filament infolist?


I have a 'role' resource which has a view route, ie:

RoleResource.php:

class RoleResource extends Resource
{
    protected static ?string $model = Role::class;

    public static function infolist(Infolist $infolist): Infolist
    {
        return $infolist
            ->schema([
                Section::make([
                    TextEntry::make('title'),
                    TextEntry::make('url'),
                ]),
            ]);
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListRoles::route('/'),
            'create' => Pages\CreateRole::route('/create'),
            'edit' => Pages\EditRole::route('/{record}/edit'),
            'view' => Pages\ViewRole::route('/{record}'),
        ];
    }
}

'ViewRole.php`:

class ViewRole extends ViewRecord
{
    protected static string $resource = RoleResource::class;
}

This results in a view page with the title 'View Role':

A view page with the title 'View Role'

How can I change this to show a different, dynamic title (such as 'View {title}')?


Solution

  • To change the title of a page to be dynamic, you can override getTitle function on the page class (ViewRole.php) like this:

    use Illuminate\Contracts\Support\Htmlable;
    
    public function getTitle(): string|Htmlable
    {
        $title = 'Role ' . ($this->record?->name);
        return "View {$title}";
    }