Creating CRUD Operations using Filament in Laravel
Introduction
- Focus: Creating CRUD pages for an application using Filament.
- CRUD: Create, Read, Update, Delete operations.
- Tools: PHP, Laravel, Filament.
Using PHP Artisan
- Command:
php artisan make:filament-resource
- Options: Requires a model name, e.g., User.
- Output: Creates new files under
app/Filament/Resources
.
Filament Resource Structure
- Files Created:
UserResource.php
- Main File: Controls database schema for CRUD operations.
- Page Files: For customizing individual CRUD pages.
Defining Form and Table
- Form Method: Defines schema for create and update operations.
- Table Method: Defines what should be shown in the listing table.
Creating Forms
- Text Input:
TextInput::make('name')
- Attributes:
required()
, email()
, password()
.
- Select Dropdown:
Select::make('name')->options([...])
- Examples:
- Add name, email, and password fields.
- Add some validation (required and email checking).
- Make password field hidden (not plain text).
- Dropdown for selecting predefined options.
CRUD Table Display
- Text Column:
TextColumn::make('name')
- Also can be used for email and ID.
- Output: Displays user data in tabular form.
Additional Features
- Password Handling: Automatic hashing with Laravel 10.
- Visibility Control:
readOnly()
, readOnlyOn([...])
, visible()
, visibleOn([...])
Customizing Icons
- Navigation Icons: Controlled by the
navigationIcon
property.
- Heroicons: Set of SVG icons used by default.
- Outline (
o
)
- Solid (
s
)
- Mini (
m
)
- Example: Replacing the default icon with various heroicons.
Resources
- Filament Documentation
- Covers forms and tables.
- Full list of supported fields (
Form Builder
and Table Builder
).
Conclusion
- Basic CRUD operations set up using Filament is straightforward.
- Extensible with numerous field types and customization options.
- Community support for queries and further examples.
Next steps: Continue exploring Filament's capabilities by delving deeper into more complex fields and advanced customizations.