Work Diary #9 – Laravel Filament

Enums can be super powerful …

… use them in factories

'region' => fake()->randomElement(Region::class),

Filament Forms are really robust.

Forms\Components\Select::make('region')

->enum(Region::class) // <-- This enforces validation!

->options(Region::class)

->required(),

These days you really don’t want to spend any time doing form validation. It’s already been done. I like the above because it will ensure that even on frontend tampering it still means server side validation will enforce the values to be one of the enums. Honestly, that’s a small chunk of boilerplate that you don’t need to do.

Laravel Shift :: Blueprint

Blueprint gets the migrations for you.

Get Filament To Generate The CRUD UI For You

php artisan make:filament-resource Venue --generate

Dependant Select Pickers

Since Filament is a LiveWire app, all form elements are set with wire:model, with none of them being live updates. But you can easily change them live which means on any change, we send a request to the server and re-render the component. This gives the opportunity to query for options. Such as:

Forms\Components\Select::make('region')

->live()

->enum(Region::class)

->options(Region::class)

->required(),

Forms\Components\Select::make('venue_id')

->relationship('venue', 'name', modifyQueryUsing: function (Builder $query, Forms\Get $get) {

ray($get('region'));

return $query->where('region', $get('region'));

}),

The keys here are:

  • Using a custom closure in modifyQueryUsing
  • Passing the Forms\Get $get method into that closure
  • Using a query on region to match the selected region value

Spatie Ray Debugging

Debugging makes software programming so much easier. The Spatie crew have made ‘Ray’ which is a real-time logging utility. I did buy Herd Pro for this purpose, but I will probably end up up returning the Hero Pro licence since it’s not as useful as I thought … and stick with Ray. Highly recommend ray

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *