tools

Filament: the best TALL Stack Package

December 16, 2022

//

3 min read

The filament is a collection of tools for rapidly building beautiful TALL Stack apps, designed for humans. - https://filamentphp.com/

The filament is the open-source alternative of the Laravel-Nova package.

Filament v2 is an admin panel builder at its core. In v3, the Filament team wants to make it an App framework instead of an admin panel builder.

With Filament, you can build Admin panels (and soon the entire app) with the minimum amount of code you can imagine.

look at these code snippets :

1<?php
2 
3namespace App\Filament\Resources;
4 
5use App\Filament\FilamentSidebar;
6use App\Filament\Resources\UserResource\Pages;
7use App\Models\Enums\GenderTypes;
8use App\Models\User;
9use Ariaieboy\FilamentJalaliDatetimepicker\Forms\Components\JalaliDateTimePicker;
10use Filament\Forms;
11use Filament\Resources\Form;
12use Filament\Resources\Resource;
13use Filament\Resources\Table;
14use Filament\Tables;
15 
16class UserResource extends Resource
17{
18 protected static ?string $model = User::class;
19 
20 protected static ?string $modelLabel = 'user';
21 
22 protected static ?string $pluralModelLabel = 'users';
23 
24 protected static ?string $navigationGroup = 'management';
25 
26 protected static ?int $navigationSort = 0;
27 
28 protected static ?string $navigationIcon = 'fad-users';
29 
30 public static function form(Form $form): Form
31 {
32 return $form
33 ->schema([
34 Forms\Components\TextInput::make('first_name')
35 ->maxLength(125),
36 Forms\Components\TextInput::make('last_name')
37 ->maxLength(125),
38 Forms\Components\TextInput::make('email')
39 ->unique(ignoreRecord: true)
40 ->email()
41 ->extraInputAttributes(['autocomplete' => 'off'])
42 ->maxLength(125),
43 Forms\Components\TextInput::make('phone')
44 ->afterStateHydrated(function ($component, $state) {
45 $component->state((string) $state);
46 })
47 ->unique(ignoreRecord: true)
48 ->tel()
49 ->required()
50 ->maxLength(125),
51 JalaliDateTimePicker::make('email_verified_at'),
52 Forms\Components\TextInput::make('password')
53 ->password()
54 ->extraInputAttributes(['autocomplete' => 'new-password'])
55 ->minLength(8)
56 ->maxLength(125),
57 JalaliDateTimePicker::make('birth_date')->weekStartsOnSunday(),
58 Forms\Components\Select::make('gender')->options(GenderTypes::toNameArray()),
59 Forms\Components\Select::make('roles')
60 ->multiple()
61 ->relationship('roles', 'name')
62 ->preload(),
63 Forms\Components\Toggle::make('completed')
64 ->required(),
65 ]);
66 }
67 
68 public static function table(Table $table): Table
69 {
70 return $table
71 ->columns([
72 Tables\Columns\TextColumn::make('id')->sortable(),
73 Tables\Columns\TextColumn::make('full_name'),
74 Tables\Columns\TextColumn::make('email'),
75 Tables\Columns\TextColumn::make('phone')->formatStateUsing(fn ($state) => $state->get()),
76 Tables\Columns\BooleanColumn::make('completed'),
77 Tables\Columns\TextColumn::make('gender')->enum(GenderTypes::toNameArray()),
78 ])
79 ->actions([
80 Tables\Actions\EditAction::make(),
81 Tables\Actions\DeleteAction::make(),
82 ])
83 ->bulkActions([
84 Tables\Actions\DeleteBulkAction::make(),
85 ]);
86 }
87 
88 public static function getPages(): array
89 {
90 return [
91 'index' => Pages\ListUsers::route('/'),
92 'create' => Pages\CreateUser::route('/create'),
93 'edit' => Pages\EditUser::route('/{record}/edit'),
94 ];
95 }
96}

The Code above is the only thing you need to create a full CRUD for your User Model.

Even if it's your first time seeing a Filament resource file you can understand 90% of the snippet's code above. Because of the great syntax of the Filament.

One of the best features of Filament is the auto-populating of select options using the model relationship:

1Forms\Components\Select::make('roles')
2 ->multiple()
3 ->relationship('roles', 'name')
4 ->preload(),

In these snippets, I've created a multiple select input for the roles relationship of my User model. with only 4 lines of code, it's gonna populate my select options using the roles relationship of my User model. It's getting, even more, interesting because these 4 lines of code also handle the creation or updating of the record relationship automatically and you don't need to do anything to save the selected options that users send with the create or edit form and that is amazing.

Your relationship may have a lot of options and good practice is to add a search to the select something like Select2js is very useful in a typical application you must install and configure the Select2 and provide the needed data or create an API endpoint so Select2 can fetch the data remotely. but in the Filament the only thing that you need to do is to add one line of code.

1Forms\Components\Select::make('roles')
2 ->multiple()
3 ->searchable()
4 ->relationship('roles', 'name')
5 ->preload(),

By adding one method searchable() you gonna have a Select2 kind of search on your select input. isn't that amazing?

If your relationship is more complicated than a simple select input you can use Relation managers to handle the relationship between your resources.

Filament also gives you the option to use its packages outside of the Filament Admin if your application is using the TALL Stack.

Right now there are 3 plugins you can use outside of filament admin. Form builder, Table builder and Notifications.

And it's not the end of the story Filament has a lot of third-party plugins you can use. right now that I am writing this article there are more than 140 plugins for Filament that you can find them here.

Filament also has a nice Tricks section on their website where users share Tips and Tricks about how you can do things using Filament and Tall Stack.

I hope this article helped you find great tools. You can share the tools You're using with me on Twitter.