How to Output a Text Formatted with Line Breaks in a Filament Admin Panel (for Laravel apps)

Categories
Web Development Today I learned

While building a Filament Panel in an internal Laravel app today, I wanted to display a read only view of my Model. It contains a “message” field that has line breaks in it, which I wanted to show correspondingly.

I achieved this by combining the formatStateUsing() and html() methods with php’s nl2br function on a TextEntry:

<?php 

public static function infolist(Infolist $infolist): Infolist
{
    return $infolist
        ->schema([
            TextEntry::make('message')
                ->formatStateUsing(fn (string $state) => nl2br($state))
                ->html(),
        ]);
}

Dein Kommentar

Hinweis: Deine E-Mailadresse wird nicht veröffentlicht.

This site uses Akismet to reduce spam. Learn how your comment data is processed.