I created an easy to use API to help businesses do incredible things with AI.
Filament

How to Format Text Columns in Filament

Benjamin Crozat
Published on Jun 20, 2024 0 comments Edit on GitHub
How to Format Text Columns in Filament

Introduction

Have you ever looked at your Filament admin panel and thought, “These text columns could use a bit of sprucing up”? Well, you’re in luck! Today, that’s what I want to write about. Properly formatted text columns can dramatically improve readability and make your admin panel look more professional.

Setting Up Your First Formatted Text Column

Let’s start with the basics. Creating a text column in Filament is pretty straightforward:

use Filament\Tables\Columns\TextColumn;

TextColumn::make('title')

This will display the “title” field as is. But we want to add some swag, right? That’s where the formatStateUsing() method comes in handy:

use Illuminate\Support\Str;

TextColumn::make('title')
    ->formatStateUsing(fn (string $state) => Str::title($state))

Now we’re talking! This will display the title in title case. But hold on, there’s something important we need to discuss…

The $state Variable: A Crucial Detail for Formatting

Here’s a little gotcha that might trip you up: when using formatStateUsing(), always use $state as your variable name. It might be tempting to use something like $title, but resist that urge! If you use any other name, Filament will throw a fit (technically, a Illuminate\Contracts\Container\BindingResolutionException).

For example, this will cause an error:

// Don't do this!
->formatStateUsing(fn (string $title) => strtoupper($title))

Always stick with $state.

Practical Examples: Formatting Text Columns in Filament

Now that we’ve got the basics down, let’s look at some practical ways to format your text columns:

  1. Capitalizing the first letter:

    ->formatStateUsing(fn (string $state) => ucfirst($state))
    
  2. Truncating long strings:

    ->formatStateUsing(fn (string $state) => Str::limit($state, 50))
    
  3. Adding a prefix:

    ->formatStateUsing(fn (string $state) => "Article: {$state}")
    
  4. Formatting dates:

    ->formatStateUsing(fn (string $state) => Carbon::parse($state)->format('M d, Y'))
    

Best Practices for Formatting Text Columns in Filament

While formatting in Filament is powerful, remember that it’s applied on the fly. For data that’s always displayed in a certain format, consider storing it that way in the database instead. It can be more efficient, especially for large datasets.

Conclusion

And there you have it! You’re now equipped to turn those plain text columns into formatted masterpieces. Remember, the key is to use $state, get creative with your formatting, and always consider the end-user experience.

Be the first to comment!

Get help or share something of value with other readers!

Great deals for enterprise developers
  • Summarize and talk to YouTube videos. Bypass ads, sponsors, chit-chat, and get to the point.
    Try Nobinge →
  • Monitor the health of your apps: downtimes, certificates, broken links, and more.
    20% off the first 3 months using the promo code CROZAT.
    Try Oh Dear for free
  • Keep the customers coming; monitor your Google rankings.
    30% off your first month using the promo code WELCOME30
    Try Wincher for free →
The latest community links
- / -