Creating Resources in Filament (Laravel Framework)
Filament is a Laravel-based framework that provides an intuitive admin panel for managing your application. One of its core features is the ability to create resources that serve as a bridge between your database and the admin interface. In this guide, we’ll walk you through the process of creating and customizing resources in Filament.
What Are Filament Resources?
Filament resources are essentially classes that define how your database models are displayed, managed, and interacted with in the admin panel. They include configurations for:
CRUD operations (Create, Read, Update, Delete).
- Forms for data entry.
- Tables for data display.
Pre-requisites
Before starting, ensure you have:
1. Laravel installed and configured.
2. The Filament package installed. If not, run:
composer require filament/filament
3. A database migration and model already set up for the entity you want to manage.
Step 1: Create a New Resource
To generate a resource, use the following Artisan command:
php artisan make:filament-resource Post
Here, replace Post with the name of your model. If your model doesn’t exist, Filament will prompt you to create it.
This command generates:
A resource class at app/Filament/Resources/PostResource.php.
A form configuration file.
A table configuration file.
Step 2: Understand the Resource Structure
After generation, you’ll find the following key files:
PostResource.php: Main resource class defining CRUD operations.
PostResource/Pages: Includes page classes (e.g., ListPosts, EditPost, CreatePost).
PostResource/Widgets: (Optional) Allows adding widgets for dashboards.
Step 3: Configure the Resource
Define the Model
In PostResource.php, link the resource to your Eloquent model:
protected static ?string $model = Post::class;
Customize the Navigation
You can modify how the resource appears in the admin panel's sidebar:
protected static ?string $navigationLabel = 'Blog Posts';
protected static ?string $navigationIcon = 'heroicon-o-document-text';
protected static ?int $navigationSort = 1;
Step 4: Set Up Forms
Forms define how your model's fields are displayed and validated during Create/Update operations. In PostResource.php, locate the form() method:
public static function form(Form $form): Form
{
return $form
->schema([
TextInput::make('title')
->required()
->maxLength(255),
Textarea::make('content')
->required(),
FileUpload::make('image')
->image(),
]);
}
Here:
TextInput, Textarea, and FileUpload define the input fields.
The required() and maxLength() methods add validation rules.
Step 5: Set Up Tables
Tables define how your data is displayed in a list view. In PostResource.php, locate the table() method:
public static function table(Table $table): Table
{
return $table
->columns([
TextColumn::make('title')->sortable()->searchable(),
ImageColumn::make('image'),
TextColumn::make('created_at')->dateTime(),
])
->filters([
Filter::make('Published')
->query(fn (Builder $query) => $query->whereNotNull('published_at')),
]);
}
Columns: Display fields in the table (e.g., title, image).
Filters: Add quick filtering options for the data.
Step 6: Custom Pages (Optional)
To add custom pages to your resource, use:
php artisan make:filament-page CustomPage --resource=PostResource
This generates a new page class under PostResource/Pages.
Step 7: Test the Resource
Visit your Filament admin panel (e.g., http://your-app.test/admin) to see your resource in action. You should now be able to create, view, edit, and delete records.
Step 8: Further Customization
Permissions: Integrate user roles and permissions for resource access.
Global Search: Enable your resource to appear in global search by overriding the `
Comments
Post a Comment