Kritim Yantra
Jun 13, 2025
Every web app needs Create, Read, Update, and Delete (CRUD) operations. Whether you're building:
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!
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.
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
Filament automates CRUD generation!
php artisan make:filament-resource Post
This creates:
app/Filament/Resources/PostResource.php
app/Models/Post.php
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(),
]);
}
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);
}
Always validate inputs:
Forms\Components\TextInput::make('title')
->required()
->maxLength(255),
In PostResource.php
, eager load relationships:
public static function getEloquentQuery(): Builder
{
return parent::getEloquentQuery()->with('user');
}
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;
}
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(),
]),
]);
}
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,
];
Visit /admin/posts
and try:
Everything should work smoothly!
🎯 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.
Now you can build efficient, secure CRUD systems in Laravel 12 with Filament! 🚀
Questions? Drop them below! Happy coding! 💻
No comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google