Skip to content

ThijmenKort/filament-badgeable-column

 
 

Repository files navigation

Filament Badgeable Column

Latest Version on Packagist Total Downloads

With Filament Badgeable Column you append badges to your columns.

Light View

Dark View

With Tags View

Installation

You can install the package via composer:

composer require awcodes/filament-badgeable-column

Optionally, you can publish the views, translations and assets using

php artisan vendor:publish --tag="filament-badgeable-column-views"
php artisan vendor:publish --tag="filament-badgeable-column-translations"
php artisan vendor:publish --tag="filament-badgeable-column-assets"

Usage

BadgeableColumn

use Awcodes\FilamentBadgeableColumn\Components\Badge;
use Awcodes\FilamentBadgeableColumn\Components\BadgeField;
use Awcodes\FilamentBadgeableColumn\Components\BadgeableColumn;

return $table
    ->columns([
        BadgeableColumn::make('title')
            ->badges([
                Badge::make('front_page')
                    ->label('Front Page')
                    ->color('success')
                    ->visible(fn ($record): bool => $record->front_page),
                Badge::make('front_page_custom_color')
                    ->label('Badass')
                    ->color('#bada55')
                    ->visible(fn ($record): bool => $record->front_page),
                Badge::make('trashed')
                    ->label('Trashed')
                    ->color('danger')
                    ->visible(fn ($record): bool => $record->deleted_at ?? false),
                BadgeField::make('status')
                    ->options([
                        'Draft' => 'Draft',
                        'Review' => 'In Review',
                        'Published' => 'Published'
                    ])
                    ->colors([
                        'gray' => 'Draft',
                        'warning' => 'Review',
                        'success' => 'Published',
                    ])
                    ->visible(fn ($record): bool => $record->status !== Status::Published->name)
            ])
            ->searchable()
            ->sortable(),
    ]);

You can also define the array of badges via a closure, if you want the array of badges to be based on dynamic data. The closure should return an array of Badge or BadgeField objects, similar to above.

The example below assumes the records have a BelongsToMany relationship called topics, and shows how to display each topic name as a badge.

use Awcodes\FilamentBadgeableColumn\Components\Badge;
use Awcodes\FilamentBadgeableColumn\Components\BadgeField;
use Awcodes\FilamentBadgeableColumn\Components\BadgeableColumn;

return $table
    ->columns([
        BadgeableColumn::make('title')
            ->badges(function($record) {
                  return $record->topics->map(function($topic) {
                    return Badge::make($topic->name)->color($topic->color)
                  });
            })
            ->searchable()
            ->sortable(),
    ]);

Badgeable Tags Column

This is similar to the Badgeable Column except it allows you to use an array of data to simply output badges in the column. You field must return an array from the record.

use Awcodes\FilamentBadgeableColumn\Components\BadgeableTagsColumn;

BadgeableTagsColumn::make('tags')
    ->colors([
        'gray',
        'primary' => 'Dan',
        '#bada55' => 'Zep',
        'warning' => 'Dennis',
        '#0e7490' => 'Ryan',
    ]),

Wrapping

If you need to wrap the badges to keep your columns from getting too wide you can use the wrapEvery() method to tell the column where to break.

use Awcodes\FilamentBadgeableColumn\Components\BadgeableTagsColumn;

BadgeableTagsColumn::make('tags')
    ->wrapEvery(3)

Badge Shape

If you prefer to have a more "square" shape you can use the asPills() method to set the shape of the badges. The default is that each badge will be a pill shape.

use Awcodes\FilamentBadgeableColumn\Components\BadgeableTagsColumn;

BadgeableTagsColumn::make('tags')
    ->asPills(false)
    ->colors([
        'gray',
        'primary' => 'Dan',
        '#bada55' => 'Zep',
        'warning' => 'Dennis',
        '#0e7490' => 'Ryan',
    ]),

Badge Text Color

You may customize the color of the label for a badge with the textColor method. This only works with custom colors.

Badge::make('front_page_custom_color')
    ->label('Badass')
    ->color('#bada55')
    ->textColor('#ff0000')

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

About

Filament Tables column with appendable badges.

Resources

License

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 60.9%
  • Blade 36.0%
  • JavaScript 3.0%
  • CSS 0.1%