Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Laravel Filament sample code


Laravel Filament sample code


Here is a concise sample of how to create a basic admin panel using Laravel Filament:

Installation

1. Install Laravel Filament via Composer:

bash
composer require filament/filament:"^3.0-stable" -W

2. Publish the Filament assets:

bash
php artisan filament:install

3. Create a Filament user:

bash
php artisan make:filament-user

Creating a Resource

1. Generate a Filament resource for a model:

bash
php artisan make:filament-resource Post

2. Define the resource fields in `app/Filament/Resources/PostResource.php`:

php
use Filament\Forms;
use Filament\Resources\Form;
use Filament\Resources\Resource;
use Filament\Resources\Table;
use Filament\Tables;

class PostResource extends Resource
{
    static $model = Post::class;

    static $view = 'filament.resources.post-resource';

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\TextInput::make('title')
                    ->required()
                    ->maxLength(255),
                Forms\Components\RichEditor::make('content')
                    ->required(),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('title'),
                Tables\Columns\TextColumn::make('created_at')
                    ->dateTime(),
            ])
            ->filters([
                Tables\Filters\TrashedFilter::make(),
            ]);
    }
}

3. Visit `/admin/posts` in your browser to see the admin panel for managing posts[1][2][3].

This example demonstrates how to:

- Install Filament and create an admin user
- Generate a Filament resource for a model
- Define form fields for creating/editing records
- Define table columns and filters for listing records

Filament provides a powerful set of tools to rapidly build admin panels with minimal code[3][4]. It leverages Laravel's conventions and the TALL stack (Tailwind CSS, Alpine.js, Livewire, Laravel) to create a modern, dynamic, and user-friendly admin interface[3][5].

Citations:
[1] https://www.youtube.com/watch?v=6c4jVLWmDYU
[2] https://dev.to/snehalkadwe/filament-v3-with-laravel-10-3h9k
[3] https://filamentphp.com
[4] https://github.com/filamentphp/demo
[5] https://redberry.international/laravel-filament-create-admin-panel/