Getting started
Overview
Resources are static classes that are used to build CRUD interfaces for your Eloquent models. They describe how administrators should be able to interact with data from your app - using tables and forms.
Creating a resource
To create a resource for the App\Models\Customer
model:
php artisan make:filament-resource Customer
This will create several files in the app/Filament/Resources
directory:
.
+-- CustomerResource.php
+-- CustomerResource
| +-- Pages
| | +-- CreateCustomer.php
| | +-- EditCustomer.php
| | +-- ListCustomers.php
Your new resource class lives in CustomerResource.php
.
The classes in the Pages
directory are used to customize the pages in the app that interact with your resource. They're all full-page Livewire components that you can customize in any way you wish.
Have you created a resource, but it's not appearing in the navigation menu? If you have a model policy, make sure you return
true
from theviewAny()
method.
Simple (modal) resources
Sometimes, your models are simple enough that you only want to manage records on one page, using modals to create, edit and delete records. To generate a simple resource with modals:
php artisan make:filament-resource Customer --simple
Your resource will have a "Manage" page, which is a List page with modals added.
Additionally, your simple resource will have no getRelations()
method, as relation managers are only displayed on the Edit and View pages, which are not present in simple resources. Everything else is the same.
Automatically generating forms and tables
If you'd like to save time, Filament can automatically generate the form and table for you, based on your model's database columns, using --generate
:
php artisan make:filament-resource Customer --generate
Handling soft deletes
By default, you will not be able to interact with deleted records in the app. If you'd like to add functionality to restore, force delete and filter trashed records in your resource, use the --soft-deletes
flag when generating the resource:
php artisan make:filament-resource Customer --soft-deletes
You can find out more about soft deleting here.
Generating a View page
By default, only List, Create and Edit pages are generated for your resource. If you'd also like a View page, use the --view
flag:
php artisan make:filament-resource Customer --view
Specifiying a custom model namespace
By default, Filament will assume that your model exists in the App\Models
directory. You can pass a different namespace for the model using the --model-namespace
flag:
php artisan make:filament-resource Customer --model-namespace=Custom\\Path\\Models
In this example, the model should exist at Custom\Path\Models\Customer
. Please note the double backslashes \\
in the command that are required.
Now when generating the resource, Filament will be able to locate the model and read the database schema.
Record titles
A $recordTitleAttribute
may be set for your resource, which is the name of the column on your model that can be used to identify it from others.
For example, this could be a blog post's title
or a customer's name
:
protected static ?string $recordTitleAttribute = 'name';
This is required for features like global search to work.
You may specify the name of an Eloquent accessor if just one column is inadequate at identifying a record.