Courses

Filament 3 From Scratch: Practical Course

Table Columns for Live-Editing Data

Summary of this lesson:
- Implementing toggle columns
- Adding checkbox columns
- Creating select columns
- Managing text input columns

Filament builds tables not only for showing data but also for live editing of some of the columns, and there are specific column types for it. We will look at them in this short lesson.


Toggle Column

Probably one of the most naturally looking fields for editing is the on/off toggle: from a UX perspective, admin panel users will immediately understand that it's clickable.

Let's add a field of whether a product is active and make it "toggleable".

Migration code:

Schema::table('products', function (Blueprint $table) {
$table->boolean('is_active')->default(true);
});

app/Models/Product.php:

class Product extends Model
{
protected $fillable = [
'name',
'price',
'status',
'category_id',
'is_active'
];

ProductResource.php:

return $table
->columns([
Tables\Columns\TextColumn::make('name'),
Tables\Columns\TextColumn::make('price'),
Tables\Columns\ToggleColumn::make('is_active'),

And that's it. You don't need to configure any more actions.

Data update works in live mode immediately when you click the column to change the value.


Checkbox Column

Very similar behavior to a ToggleColumn, just a different look.

Let's use the same is_active column but present it as a checkbox instead of a Toggle.

ProductResource.php:

return $table
->columns([
Tables\Columns\TextColumn::make('name'),
Tables\Columns\TextColumn::make('price'),
Tables\Columns\CheckboxColumn::make('is_active'),

That's all you need to change!

Here's how it looks:


Select Column

Sometimes we just want to change some status in a simple dropdown select but don't want to click Edit and go to a separate page just for that.

Select could be implemented directly in the table. Let's try to do it with the status column.

All we need to do is repeat the same pattern as in the SelectInput in the form, just with SelectColumn in the table:

ProductResource.php:

return $table
->columns([
// ...
Tables\Columns\CheckboxColumn::make('is_active'),
Tables\Columns\SelectColumn::make('status')
->options(self::$statuses),

And it works!

Again, the data is updated immediately on dropdown value change.


Text Input Column

Finally, occasionally, you may want a simple text directly editable in the table.

To achieve that, just use TextInputColumn instead of TextColumn.

ProductResource.php:

return $table
->columns([
Tables\Columns\TextColumn::make('name'),
Tables\Columns\TextInputColumn::make('name'),

You also should add any validation rules here. As usual, chain the method for it.

Tables\Columns\TextInputColumn::make('name')
->rules(['required', 'min:3']),

In case of a validation error, the error text will appear as a tooltip.

Personally, I'm not a big fan of such a text-edit solution because text field validation may be pretty tricky and complicated.


In general, such editable fields in the table may slow down the loading of the whole table. But hey, it's your choice, Filament just offers an option!

avatar

Thank you for this very interesting course! One little thing: In the last code block, the line ->rules(['required', 'min:3']), should be marked as added (green) instead of removed (red).

👍 2
avatar

Well noticed! Fixed now.

avatar
You can use Markdown
avatar

There is a typo in the lesson intro. "Filament builds tables now only for showing data" should be "Filament builds tables not only for showing data".

avatar

Thanks, well noticed! Fixed now.

avatar
You can use Markdown
avatar

The TextColumn->TextInputColumn snippet has both lines shown in red as if they are both deleted, instead of the 2nd one being green to show addition.

First course I go through. I really like how many times you say "Yes, it's that simple" or equivalent, and I agree. Filament made quite an impression on you, hasn't it?

avatar

Thanks! Fixed the TextInputColumn.

And yes, with Filament it's quite emotional: I'm still surprised by how easy many things are, and how deep is the tool to discover even more methods/ways to achieve the same things even easier.

avatar
You can use Markdown
avatar

Thanks

This also works with BelongsTo relationships in the following way:

	Tables\Columns\TextInputColumn::make('country.name'),

But is there a way to make this work with HasMany?

avatar

Hi, this should work out for you:

https://filamentphp.com/docs/3.x/tables/columns/text#listing-multiple-values

avatar

Hello,

Thanks for the link. But this is only for reading, for TextColumn Filament\Tables\Columns\TextColumn But I need it for editing, for TextInputColumn Tables\Columns\TextInputColumn

I partially got it to work like this:

model HasMany relation

	 
    public function commissions(): HasMany
    {
        return $this->hasMany(Commission::class);
    }

And in Filament Resources:


    public static function table(Table $table): Table
    {
        $columns = [
            Tables\Columns\TextColumn::make('id')->searchable(),
            Tables\Columns\TextColumn::make('commissions.value'),
        ];

        foreach (range(0,3) as $id){
            $columns[] = Tables\Columns\TextInputColumn::make('commissions.'.$id.'.value')->label('commissions.'.$id.'.value');
        }

        return $table
            ->columns( $columns);
    }

This displays the correct values, displays fields for editing. But saving itself doesn't work =/

I also refer to a fixed number of elements via range(0,3) , but I would like to do this more elegantly, more dynamically.

And in this use make('commissions.'.$id.'.value') - $id is just a serial number, not a unique identifier, which looks very strange... Although it works for display

avatar

Oh sorry, I missed the part where this was an input!

In this case, sorry, I don't really know the answer. It seems pretty weird to be to add this to the table, so I never did that :)

avatar
You can use Markdown
avatar
Loganathan Natarajan

Thanks, simple and neat explanation

avatar
You can use Markdown
avatar
You can use Markdown