Latest Premium Tutorial

Latest Course

Newest content

  • This Week: 40% Discount for Yearly and Lifetime Plans!
    · 2 mins, 215 words

    This Week: 40% Discount for Yearly and Lifetime Plans!

  • Laravel 11: New Artisan "make:trait" Command
    · 1 min, 107 words

    Laravel 11: New Artisan "make:trait" Command

  • Random Quick Laravel Tip:
    Filament: Badge Columns with Enums

    If you have a set of options for radio/dropdowns in the Form, you may want to create a PHP Enum class. It will give you the benefit of re-using the same class and also automatically show colors/icons as a badge.

    Before:

    ProductResource.php:

    public static function form(Form $form): Form
    {
    return $form
    ->schema([
    // ... other fields
    Forms\Components\Radio::make('status')
    ->options([
    'in stock' => 'in stock',
    'sold out' => 'sold out',
    'coming soon' => 'coming soon',
    ]),
    ]);
    }

    After:

    app/Enums/ProductStatus.php:

    namespace App\Enums;
     
    use Filament\Support\Contracts\HasIcon;
    use Filament\Support\Contracts\HasLabel;
    use Filament\Support\Contracts\HasColor;
     
    enum ProductStatus: string implements HasLabel, HasColor, HasIcon
    {
    case IN_STOCK = 'in stock';
    case SOLD_OUT = 'sold out';
    case COMING_SOON = 'coming soon';
     
    public function getLabel(): ?string
    {
    return match ($this) {
    self::IN_STOCK => 'In stock',
    self::SOLD_OUT => 'Sold out',
    self::COMING_SOON => 'Coming soon',
    };
    }
     
    public function getColor(): string|array|null
    {
    return match ($this) {
    self::IN_STOCK => 'success',
    self::SOLD_OUT => 'warning',
    self::COMING_SOON => 'gray',
    };
    }
     
    public function getIcon(): ?string
    {
    return match ($this) {
    self::IN_STOCK => 'heroicon-m-pencil',
    self::SOLD_OUT => 'heroicon-m-eye',
    self::COMING_SOON => 'heroicon-m-check',
    };
    }
    }

    app/Models/Product.php

    use App\Enums\ProductStatus;
     
    class Product extends Model {
    // ...
     
    protected $casts = [
    'status' => ProductStatus::class,
    ];
    }

    ProductResource.php:

    use App\Enums\ProductStatus;
     
    public static function form(Form $form): Form
    {
    return $form
    ->schema([
    // ... other fields
    Forms\Components\Radio::make('status')
    ->options(ProductStatus::class),
    ]);
    }

    It feels like a lot of work, right? But look at those methods getLabel(), getColor() and getIcon(). Now, in the Table of the Resource, you can just use the badge() method, and Enum will auto-style it for you.

    public static function table(Table $table): Table
    {
    return $table
    ->columns([
    // ... other columns
    Tables\Columns\TextColumn::make('status')
    ->badge(),
    ]);
    }

    Read all 376 tips on GitHub

  • Laravel 11: New Artisan "make:interface" Command
    · 1 min, 110 words

    Laravel 11: New Artisan "make:interface" Command

  • Laravel 11: New Artisan "make:enum" Command
    · 1 min, 159 words

    Laravel 11: New Artisan "make:enum" Command

  • Laravel 11: New Artisan "make:class" Command
    · 1 min, 194 words

    Laravel 11: New Artisan "make:class" Command

  • How to Structure Laravel 11 Projects

    · 11041 words

    Premium Course: How to Structure Laravel 11 Projects

  • Laravel 11: Main New Features and Changes
    · 7 mins, 1314 words

    Laravel 11: Main New Features and Changes

  • Laravel 11: How to Change Default SQLite to MySQL
    · 1 min, 171 words

    Laravel 11: How to Change Default SQLite to MySQL

  • Caching in Laravel with Redis: Simple Example
    · Updated Mar 2024 · 3 mins, 477 words

    Caching in Laravel with Redis: Simple Example

  • Virtual DB Columns in Laravel Migrations and MySQL
    · 5 mins, 890 words

    Virtual DB Columns in Laravel Migrations and MySQL

  • CarbonImmutable Class: Why You Would Need It
    · 2 mins, 291 words

    CarbonImmutable Class: Why You Would Need It