Laravel 12 + Filament: CRUD Operations with Best Practices

Author

Kritim Yantra

Jun 13, 2025

Laravel 12 + Filament: CRUD Operations with Best Practices

Every web app needs Create, Read, Update, and Delete (CRUD) operations. Whether you're building:

  • A blog (posts, comments)
  • An e-commerce site (products, orders)
  • A task manager (tasks, categories)

CRUD is the backbone of your application.

In this guide, you'll learn how to:
Generate CRUD operations in Laravel 12 using Filament.
✅ Follow best practices for clean, maintainable code.
✅ Optimize performance and security.

Let’s get started!


1. Setting Up Laravel 12 with Filament

Prerequisites

  • PHP 8.2+
  • Composer
  • Laravel 12 installed

Step 1: Install Laravel & Filament

composer create-project laravel/laravel filament-crud-demo
cd filament-crud-demo
composer require filament/filament:"^3.2"
php artisan filament:install --panels
php artisan make:filament-user

Visit /admin and log in.


2. Creating a Model, Migration & Filament Resource

Example: A "Post" CRUD System

Step 1: Generate Model & Migration

php artisan make:model Post -m

Edit the migration (database/migrations/xxxx_create_posts_table.php):

Schema::create('posts', function (Blueprint $table) {
    $table->id();
    $table->string('title');
    $table->text('content');
    $table->foreignId('user_id')->constrained();
    $table->timestamps();
});

Run the migration:

php artisan migrate

Step 2: Generate a Filament Resource

Filament automates CRUD generation!

php artisan make:filament-resource Post

This creates:

  • app/Filament/Resources/PostResource.php
  • app/Models/Post.php

3. Configuring the Filament Resource

Step 1: Define Fields in PostResource

Edit app/Filament/Resources/PostResource.php:

use Filament\Forms;
use Filament\Tables;

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

public static function table(Table $table): Table
{
    return $table
        ->columns([
            Tables\Columns\TextColumn::make('title'),
            Tables\Columns\TextColumn::make('user.name'),
        ])
        ->filters([])
        ->actions([
            Tables\Actions\EditAction::make(),
            Tables\Actions\DeleteAction::make(),
        ]);
}

Step 2: Set Up Model Relationships

In app/Models/Post.php:

public function user()
{
    return $this->belongsTo(User::class);
}

In app/Models/User.php:

public function posts()
{
    return $this->hasMany(Post::class);
}

4. Best Practices for CRUD in Filament

✅ 1. Use Validation Rules

Always validate inputs:

Forms\Components\TextInput::make('title')
    ->required()
    ->maxLength(255),

✅ 2. Optimize Queries (Avoid N+1 Problem)

In PostResource.php, eager load relationships:

public static function getEloquentQuery(): Builder
{
    return parent::getEloquentQuery()->with('user');
}

✅ 3. Soft Deletes for Safety

Prevent accidental data loss:

php artisan make:migration add_soft_deletes_to_posts --table=posts

In the migration:

$table->softDeletes();

In Post.php:

use Illuminate\Database\Eloquent\SoftDeletes;

class Post extends Model
{
    use SoftDeletes;
}

✅ 4. Bulk Actions for Efficiency

Add bulk actions in PostResource.php:

use Filament\Tables\Actions\BulkActionGroup;
use Filament\Tables\Actions\DeleteBulkAction;

public static function table(Table $table): Table
{
    return $table
        ->bulkActions([
            BulkActionGroup::make([
                DeleteBulkAction::make(),
            ]),
        ]);
}

✅ 5. Use Policies for Authorization

Create a policy:

php artisan make:policy PostPolicy --model=Post

Define permissions in PostPolicy.php:

public function delete(User $user, Post $post)
{
    return $user->id === $post->user_id;
}

Register the policy in AuthServiceProvider.php:

protected $policies = [
    Post::class => PostPolicy::class,
];

5. Testing the CRUD Operations

Visit /admin/posts and try:

  • Creating a new post
  • Editing an existing post
  • Deleting a post
  • Filtering/Searching (if added)

Everything should work smoothly!


Key Takeaways

🎯 Filament makes CRUD generation effortless with make:filament-resource.
🎯 Always validate inputs to ensure data integrity.
🎯 Optimize queries to prevent performance issues.
🎯 Use soft deletes for recoverable data.
🎯 Secure your app with policies and bulk actions.


Final Thoughts

Now you can build efficient, secure CRUD systems in Laravel 12 with Filament! 🚀

Questions? Drop them below! Happy coding! 💻

Tags

Comments

No comments yet. Be the first to comment!

Please log in to post a comment:

Sign in with Google

Related Posts