Skip to content

Latest commit

 

History

History
175 lines (154 loc) · 11.4 KB

File metadata and controls

175 lines (154 loc) · 11.4 KB

Learn Laravel Filament full tutorial: Build powerful admin

Disclaimer: This is a personal summary and interpretation based on a YouTube video. It is not official material and not endorsed by the original creator. All rights remain with the respective creators.

This document summarizes the key takeaways from the video. I highly recommend watching the full video for visual context and coding demonstrations.

Before You Get Started

  • I summarize key points to help you learn and review quickly.
  • Simply click on Ask AI links to dive into any topic you want.

AI-Powered buttons

Teach Me: 5 Years Old | Beginner | Intermediate | Advanced | (reset auto redirect)

Learn Differently: Analogy | Storytelling | Cheatsheet | Mindmap | Flashcards | Practical Projects | Code Examples | Common Mistakes

Check Understanding: Generate Quiz | Interview Me | Refactor Challenge | Assessment Rubric | Next Steps

Project Setup and Filament Installation

  • Summary: Start by creating a new Laravel project named "filament-tutorial" without a starter kit. Set up the database, run migrations for default tables, and install Filament v3 via Composer. Use the filament:install --panels command to set up panels, forms, tables, and notifications. Create an admin user and access the /admin dashboard.
  • Key Takeaway/Example: After installation, the dashboard appears with user options like theme switching and logout. No code blocks needed here, but ensure the .env file has the correct DB_DATABASE.
  • Link for More Details: Ask AI: Filament Installation

Creating Models and Migrations

  • Summary: Generate models and migrations for Country, State, City, Department, and Employee. Define fields like names, relationships (e.g., State belongs to Country), and constraints like foreign keys with cascade on delete. Run migrations to create tables.
  • Key Takeaway/Example: For Employee, include fields like first_name, last_name, address, zip_code, date_of_birth, date_hired, and foreign keys to Country, State, City, and Department.
// Example migration for Employee
$table->string('first_name');
$table->string('last_name');
$table->string('middle_name');
$table->string('address');
$table->string('zip_code');
$table->date('date_of_birth');
$table->date('date_hired');
$table->foreignId('country_id')->constrained()->cascadeOnDelete();

Generating and Customizing Filament Resources

  • Summary: Use artisan make:filament-resource to create resources for each model, optionally with --generate for auto-filled forms/tables and --view for view pages. Customize navigation icons, labels, groups, and sorting in resource classes.
  • Key Takeaway/Example: Group resources like Country, State, City, and Department under "System Management" and set navigation sort orders (e.g., Country at 1, State at 2).
// In CountryResource.php
protected static ?string $navigationIcon = 'heroicon-o-flag';
protected static ?string $navigationGroup = 'System Management';
protected static int $navigationSort = 1;

Customizing the Dashboard and Theme

  • Summary: Modify the AdminPanelProvider to change colors, fonts, logos, and favicons. Adjust resource icons and groups for better navigation.
  • Key Takeaway/Example: Set primary color to indigo and font to Inter for a custom look.
// In AdminPanelProvider.php
->colors([
    'danger' => Color::Red,
    'primary' => Color::Indigo,
])
->font('Inter')

Building Forms with Sections and Relationships

  • Summary: Define form schemas with text inputs, selects, and sections. Use relationships for selects (e.g., State belongsTo Country) and add validation like required() or maxLength(255).
  • Key Takeaway/Example: Group fields into sections like "User Name" and set column layouts.
// In EmployeeResource.php
Section::make('User Name')
    ->description('Put the user name details here.')
    ->schema([
        TextInput::make('first_name')->required(),
        TextInput::make('last_name')->required(),
    ])->columns(2)

Dependent Selects and Data Seeding

  • Summary: Implement dependent dropdowns where State options filter based on selected Country using options() with a closure and get(). Seed countries, states, cities from external data.
  • Key Takeaway/Example: Clear dependent fields on change with afterStateUpdated().
// Dependent select for State
Select::make('state_id')
    ->live()
    ->options(fn (Get $get): Collection => State::query()
        ->where('country_id', $get('country_id'))
        ->pluck('name', 'id'))
    ->afterStateUpdated(fn (Set $set) => $set('city_id', null))

Customizing Tables and Actions

  • Summary: Define table columns, filters, and actions like view, edit, delete. Add summaries and customize action labels.
  • Key Takeaway/Example: Use TextColumn for searchable/sortable fields and add delete actions.
// In EmployeeResource.php
TextColumn::make('first_name')->searchable()->sortable(),
Action::make('delete')->successNotificationTitle('Employee deleted')

Notifications and Multi-Tenancy

  • Summary: Override notifications for create/update/delete. Set up multi-tenancy with a Team model, tenant registration/profile pages, and scoped relationships.
  • Key Takeaway/Example: Use getSavedNotification() to customize messages.
// In EditEmployee.php
protected function getSavedNotification(): ?Notification
{
    return Notification::make()->success()->title('Employee updated');
}

Multiple Panels and Access Control

  • Summary: Create an "app" panel alongside "admin". Move tenancy to "app", add registration/login, and use middleware to restrict admin access based on is_admin flag.
  • Key Takeaway/Example: Add user menu items visible only to admins.
// Middleware for admin access
if (auth()->user() && auth()->user()->is_admin) {
    return $next($request);
} else {
    return redirect('/app');
}

Dashboard Widgets

  • Summary: Create stats overview, chart, and table widgets for dashboards. Scope data to tenants and use Flowbite/Filament-Charts for dynamic charts.
  • Key Takeaway/Example: Use Trend for monthly employee charts.
// In EmployeeChart.php
protected function getData(): array
{
    $employees = Trend::model(Employee::class)
        ->between(now()->startOfMonth(), now()->endOfMonth())
        ->perDay()->count();
    return ['datasets' => [['label' => 'Employees', 'data' => $employees->map(fn (TrendValue $value) => $value->aggregate)]]];
}

About the summarizer

I'm Ali Sol, a Backend Developer. Learn more: