Laravel Filament: Rapid Admin Panel Development Guide
Learn to build production-ready admin panels with Laravel Filament.
20+ years shipping production PHP systems at scale. Written from production experience, not tutorials.
- ✓Basic knowledge of Laravel (models, migrations, routing).
- ✓PHP 8.0+ and Laravel 8+ installed.
- ✓Familiarity with Composer and npm.
- ✓Understanding of MVC pattern and Eloquent ORM.
- Filament is a Laravel package for building admin panels with minimal code.
- It provides pre-built UI components like forms, tables, and widgets.
- Resources map to Eloquent models for CRUD operations.
- Custom pages and actions extend functionality.
- Includes built-in authentication and authorization.
Think of Filament as a set of pre-fabricated building blocks for an admin dashboard. Instead of writing every line of HTML and CSS from scratch, you snap together ready-made pieces like forms, tables, and buttons. It's like using LEGO to build a house instead of carving each brick from stone.
Building an admin panel from scratch is tedious and error-prone. You need to handle authentication, CRUD operations, form validation, data tables, and more. Laravel Filament simplifies this by providing a rapid development toolkit that generates beautiful, responsive admin interfaces with minimal code.
Filament is built on top of Laravel and uses Livewire for dynamic interactions. It offers a rich set of components: resources (for CRUD), forms, tables, widgets, notifications, and custom pages. With Filament, you can create a fully functional admin panel in hours instead of days.
In this tutorial, you'll learn how to install Filament, create resources for your models, customize forms and tables, add custom pages, and implement security best practices. We'll also cover real-world scenarios like handling file uploads, using relationships, and optimizing performance.
By the end, you'll be able to build production-ready admin panels that are both developer-friendly and user-friendly.
Installing Filament
To get started, you need a Laravel application (version 8 or higher). Install Filament via Composer:
``bash composer require filament/filament ``
After installation, publish the configuration and assets:
``bash php artisan filament:install --panels ``
This command creates a panel provider (e.g., app/Providers/Filament/AdminPanelProvider.php) and publishes the configuration file. You can customize the panel's path, middleware, and other settings in this file.
Next, create a user with admin privileges. Filament uses Laravel's authentication, so you can use the default User model. Run migrations and create a user:
``bash php artisan migrate php artisan tinker > \App\Models\User::create(['name' => 'Admin', 'email' => 'admin@example.com', 'password' => bcrypt('password')]); ``
Now, access your admin panel at /admin. Log in with the credentials you created. You should see the default Filament dashboard.
Key Takeaway: Filament installation is straightforward. The panel provider is the central configuration point for your admin panel.
Creating Your First Resource
Resources are the core of Filament. They map to Eloquent models and provide CRUD interfaces. Let's create a resource for a Post model.
First, create the model and migration:
``bash php artisan make:model Post -m ``
Add fields to the migration:
``php Schema::create('posts', function (Blueprint $table) { $table->id(); $table->string('title'); $table->text('content'); $table->boolean('is_published')->default(false); $table->timestamps(); }); ``
Run the migration:
``bash php artisan migrate ``
Now, create the Filament resource:
``bash php artisan make:filament-resource Post ``
This generates a resource class at app/Filament/Resources/PostResource.php. Open it and define the form and table schemas.
Form Schema:
``php public static function form(Form $form): Form { return $form ->schema([ TextInput::make('title')->required()->maxLength(255), RichEditor::make('content')->required(), Toggle::make('is_published'), ]); } ``
Table Schema:
``php public static function table(Table $table): Table { return $table ->columns([ TextColumn::make('title')->searchable(), IconColumn::make('is_published')->boolean(), TextColumn::make('created_at')->dateTime(), ]) ->filters([ Filter::make('is_published'), ]) ->actions([ EditAction::make(), DeleteAction::make(), ]) ->bulkActions([ BulkActionGroup::make([ DeleteBulkAction::make(), ]), ]); } ``
Now, navigate to /admin/posts in your browser. You'll see a fully functional CRUD interface with forms, tables, and actions.
Key Takeaway: Resources auto-generate CRUD interfaces. Customize forms and tables using Filament's rich component library.
->searchable() on columns to enable search. For large datasets, consider using ->sortable() and pagination.Customizing Forms with Validation and Relationships
Filament forms are highly customizable. You can add validation rules, handle relationships, and use advanced components.
Validation: Add rules using methods like ->required(), ->email(), ->unique(), etc. For custom rules, use ->rule(new CustomRule).
Relationships: Use Select or CheckboxList for belongs-to or many-to-many relationships. For example, if a post belongs to a category:
``php Select::make('category_id') ->relationship('category', 'name') ->required(), ``
File Uploads: Use FileUpload component:
``php FileUpload::make('featured_image') ->image() ->directory('posts') ->visibility('public'), ``
Repeater for nested forms: Use Repeater for dynamic rows:
``php Repeater::make('tags') ->relationship('tags') ->schema([ TextInput::make('name')->required(), ]), ``
Conditional fields: Use or hidden() based on other field values:visible()
``php TextInput::make('discount') ->visible(fn (Get $get) => $get('has_discount')), ``
Key Takeaway: Filament forms support validation, relationships, file uploads, and dynamic fields. Use the Get and Set utilities for conditional logic.
Building Custom Pages and Widgets
Beyond resources, Filament allows you to create custom pages and widgets for dashboards or specialized views.
Custom Pages: Create a page with:
``bash php artisan make:filament-page Settings ``
This generates a page class. You can add forms, tables, or any Livewire component. For example, a settings page with a form:
```php use Filament\Pages\Page;
class Settings extends Page { protected static ?string $navigationIcon = 'heroicon-o-cog';
protected static string $view = 'filament.pages.settings';
public $name; public $email;
public function mount(): void { $this->form->fill([ 'name' => auth()->user()->name, 'email' => auth()->user()->email, ]); }
public function submit(): void { $this->validate(); auth()->user()->update($this->form->getState()); Notification::make()->success()->title('Settings saved.')->send(); }
protected function getFormSchema(): array { return [ TextInput::make('name')->required(), TextInput::make('email')->email()->required(), ]; } } ```
Widgets: Create a widget for the dashboard:
``bash php artisan make:filament-widget StatsOverview ``
Widgets can display stats, charts, or any data. Example:
```php use Filament\Widgets\StatsOverviewWidget as BaseWidget; use Filament\Widgets\StatsOverviewWidget\Stat;
class StatsOverview extends BaseWidget { protected function getStats(): array { return [ Stat::make('Total Posts', Post::count()), Stat::make('Published Posts', Post::where('is_published', true)->count()), Stat::make('Users', User::count()), ]; } } ```
Key Takeaway: Custom pages and widgets extend Filament beyond CRUD. Use them for settings, reports, or any custom functionality.
Implementing Authorization and Roles
Filament integrates with Laravel's authorization system. You can define policies and gates to control access.
Policies: Create a policy for a model:
``bash php artisan make:policy PostPolicy --model=Post ``
Define methods like viewAny, view, create, update, delete. Then register the policy in AuthServiceProvider.
Filament Integration: Filament automatically checks policies for resources. For example, if a user cannot view a post, the edit button will be hidden.
Roles and Permissions: Use a package like spatie/laravel-permission for role-based access. Install it:
``bash composer require spatie/laravel-permission php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider" php artisan migrate ``
Create roles and assign permissions. Then, in Filament, you can check permissions in the panel provider:
``php public function panel(Panel $panel): Panel { return $panel ->authMiddleware([ Authenticate::class, 'role:admin', // custom middleware ]); } ``
Or use Filament's method in resources:can()
``php public static function canViewAny(): bool { return ``auth()->user()->can('viewAny', static::getModel()); }
Key Takeaway: Use Laravel policies and Spatie permissions for fine-grained access control. Filament respects these automatically.
can() methods to conditionally show actions.Performance Optimization and Production Deployment
When deploying Filament to production, consider these optimizations:
Caching: Run php artisan optimize to cache configurations, routes, and views. For Filament, also cache the Livewire components:
``bash php artisan livewire:discover php artisan filament:cache ``
Asset Bundling: Filament uses Vite for asset compilation. In production, ensure you run npm run build to compile assets.
Database Queries: Use eager loading to avoid N+1 queries. In table columns, use ->with(['relation']) to load relationships.
Lazy Loading: For large datasets, use lazy loading or pagination. Filament's table supports pagination out of the box.
Queue Jobs: Offload heavy tasks like file processing or email sending to queues.
Security: Ensure your .env file has APP_DEBUG=false in production. Use HTTPS and set secure cookies.
Monitoring: Use Laravel's logging and monitoring tools like Telescope or Laravel Pulse.
Key Takeaway: Optimize Filament for production by caching, using eager loading, and offloading heavy tasks.
The Case of the Missing Admin Panel: A Filament Deployment Nightmare
- Always verify that authentication middleware is applied to admin routes.
- Use Filament's built-in authentication features instead of custom middleware unless necessary.
- Test authentication in a staging environment before deploying to production.
- Review the panel configuration file after any customization.
- Implement role-based access control for sensitive actions.
php artisan route:list | grep filament to verify.DB::enableQueryLog(). Check for N+1 queries and add eager loading.php artisan storage:link. Check file size limits in php.ini and Filament config.php artisan vendor:publish --tag=filament-configphp artisan config:clear| File | Command / Code | Purpose |
|---|---|---|
| AdminPanelProvider.php | namespace App\Providers\Filament; | Installing Filament |
| PostResource.php | namespace App\Filament\Resources; | Creating Your First Resource |
| PostResource.php (form method) | public static function form(Form $form): Form | Customizing Forms with Validation and Relationships |
| Settings.php | namespace App\Filament\Pages; | Building Custom Pages and Widgets |
| PostPolicy.php | namespace App\Policies; | Implementing Authorization and Roles |
| deploy.sh | php artisan down --retry=60 | Performance Optimization and Production Deployment |
Key takeaways
Common mistakes to avoid
3 patternsNot running `php artisan filament:install` after installation
Forgetting to register policies in AuthServiceProvider
Using `all()` on large datasets in table columns
Interview Questions on This Topic
What is Filament and how does it integrate with Laravel?
Frequently Asked Questions
20+ years shipping production PHP systems at scale. Written from production experience, not tutorials.
That's Laravel. Mark it forged?
4 min read · try the examples if you haven't