In this section we will create crud (create read update and delete) app using Laravel 10 with filamentphp v3. If you are using laravel 9 or filamentphp v2 then you can read below blog.
Laravel 9 Filament CRUD Tutorial Example
Step 1: Install Laravel & Connect Database
Installing a fresh new laravel 10 application, so head over to the terminal, type the command, and create a new laravel app.
composer create-project laravel/laravel filamentphp-crud
Now, you have to connect the laravel app to the database, hence open the .env configuration file and add the database credentials as suggested below.
.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database_name
DB_USERNAME=database_user_name
DB_PASSWORD=database_password
Step 2: Install Laravel Filament
Since Livewire v3 is still in beta, set the minimum-stability in your composer.json to dev:.
"minimum-stability": "dev",
Install the Filament Panel Builder by running the following commands in your Laravel project directory.
composer require filament/filament:"^3.0-stable" -W
php artisan filament:install --panels
Create a filament user for admin.
php artisan make:filament-user
Give username, email and password.
Name:
> admin
Email address:
> admin@gmail.com
Password:
>
Success! admin@admin.com may now log in at http://localhost/admin/login.
Step 3: Login Filament admin
http://localhost:8000/admin/login
Step 4: Create Blog Model and Migration
Run below command to create Blog modal and migration.
php artisan make:model Blog -m
create_blogs_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('blogs', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('content');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('blogs');
}
};
App/Modal/Blog.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Blog extends Model
{
use HasFactory;
protected $fillable = [
'title',
'content'
];
}
run migration
php artisan migrate
Step 5: Create a Model Resource
Run below to command create a resource for the App\Models\Blog model.
php artisan make:filament-resource Blog
This will create several files in the app/Filament/Resources directory.
+-- BlogResource.php
+-- CustomerResource
| +-- Pages
| | +-- CreateBlog.php
| | +-- EditBlog.php
| | +-- ListBlogs.php
Step 6: Create a Blog Filament CRUD
Filament/Resources/BlogResource.php
<?php
namespace App\Filament\Resources;
use App\Filament\Resources\BlogResource\Pages;
use App\Filament\Resources\BlogResource\RelationManagers;
use App\Models\Blog;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Textarea;
use Filament\Tables\Columns\TextColumn;
use Filament\Forms\Components\Section;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
class BlogResource extends Resource
{
protected static ?string $model = Blog::class;
protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';
public static function form(Form $form): Form
{
return $form
->schema([
Section::make()
->schema([
TextInput::make('title')->required(),
Textarea::make('content')->required(),
])
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
TextColumn::make('id'),
TextColumn::make('title'),
TextColumn::make('content'),
])
->filters([
//
])
->actions([
Tables\Actions\EditAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
])
->emptyStateActions([
Tables\Actions\CreateAction::make(),
]);
}
public static function getRelations(): array
{
return [
//
];
}
public static function getPages(): array
{
return [
'index' => Pages\ListBlogs::route('/'),
'create' => Pages\CreateBlog::route('/create'),
'edit' => Pages\EditBlog::route('/{record}/edit'),
];
}
}
Laravel 10 with filamentphp create operation.
Laravel 10 with filamentphp blogs index.
Laravel 10 with filamentphp edit blog and delete operation.