App\Models\Customer model:
app/Filament/Resources directory:
CustomerResource.php. Resource classes register forms, tables, authorization settings, and pages associated with that model.
The classes in the Pages directory are used to customize the pages in the admin panel that interact with your resource.
By default, the model associated with your resource is guessed based on the class name of the resource. You may set the static $model property to disable this behaviour:
$label property:
Forms
Resource classes contain a staticform() method that is used to customize the forms to create and update resource records.
schema() method is used to define the structure of your form. It is an array of components, in the order they should appear in your form.
For more information, please see the page on Building Forms.
Relations
Managing Single Related Records
TheFilament\Resources\Forms\Components\BelongsToSelect field can be used in resource form schemas to create a select element with options to search and select a related record. It has the same methods available as Filament\Resources\Forms\Components\Select, and others to define the relationship and column name that should be used:
- A
category_idforeign key column exists on your parent model. - A
belongsTo()categoryrelationship on your parent model. - A
namecolumn on your category model that can be used to render the list of categories to select from.
preload() a select field with options. Please only do this if you are certain that there are only a few related records to choose from:
relationship() method.
Managing Multiple Related Records
Relation managers are components that allow administrators to list, create, attach, edit, detach and delete related records without leaving the parent record’s edit page. Resource classes contain a staticrelations() method that is used to register relation managers for your resource.
To create a relation manager, you can use:
CustomerResource/RelationManagers/OrdersRelationManager.php file. This contains a class where you are able to define a form and table for your relation manager. The relation manager will interact with the orders relationship on your parent model.
You must set the primary column of related records using the static $primaryColumn property on your new relation manager class. The primary column is used to identify related records quickly. This could be a user’s name, or a blog post’s title.
relations() method:
HasMany, BelongsToMany and MorphMany relationships are currently fully supported by relation managers.
BelongsToMany relation managers require an extra static $inverseRelationship property set on the class if the existing relationships deviate from traditional naming conventions:
Tables
Resource classes contain a statictable() method that is used to customize the table to list resource records.
columns() method is used to define the columns in your table. It is an array of column objects, in the order they should appear in your table.
Filters are predefined scopes that administrators can use to filter records in your table. The filters() method is used to register these.
For more information, please see the page on Building Tables.
Pages
Pages are classes that are associated with a resource. They are essentially Laravel Livewire components with custom integration utilities for use with Filament. Page class files are in the/Pages directory of your resource directory.
By default, resources are generated with three pages:
- List has a table for displaying, searching and deleting resource records. From here, you are able to access the create and edit pages. It is routed to
/. - Create has a form that is able to create a resource record. It is routed to
/create. - Edit has a form that is able to update a resource record, along with the relation managers registered to your resource. It is routed to
/{record}/edit.
Customizing Default Pages
You are able to customize text used in the default pages by overriding properties on the page class. To see the options available, check the static properties defined in the parent class of each default page. For further customization opportunities, you can override the static$view property on your page to a custom view in your app:
Customizing Form Redirects
You may specify a custom redirect URL for the Create and Edit pages by overriding thegetRedirectUrl() method.
For example, the Create form can redirect back to the List page when it is submitted:
Hooks
Hooks may be used to customize the behaviour of a default page. To set up a hook, create a protected method on the page class with the name of the hook:beforeSave() method will be called before the data in the form is saved to the database.
There are several available hooks for the create and edit pages:
Custom Pages
Filament allows you to create completely custom pages for resources. To create a new page, you can use:/Pages directory of your resource directory, and a view in the /pages directory of the resource views directory.
You must register custom pages to a route in the static routes() method of your resource:
routeTo() method is the path of the route, and the second is its name. Any parameters defined in the route’s path will be available to the page class, in an identical way to Livewire.
To generate a URL for a resource route, you may call the static generateUrl() method on the page class:
url() method, you can get access to each row’s record.
Authorization
For authorization, Filament will observe any model policies that are registered in your app. TheviewAny action may be used to completely disable resources and remove them from the navigation menu.
Filament also includes a powerful role-based authorization system, which is set up out of the box with the default users table. You may also implement roles functionality in a custom users table.
You may create roles, such as Manager, using:
Manager role to access a resource, declare so in the static authorization() method:
Please note: administrators will always have full access to every resource in your admin panel.You may want to only deny users with the
Manager role from accessing this resource. To do this, you may use the static deny() method instead:
only() certain actions that roles have access to. These follow the same naming conventions as methods in a policy.
except() those specified: