Creating a resource
To create a resource for theApp\Models\Customer model:
app/Filament/Resources directory:
CustomerResource.php.
The classes in the Pages directory are used to customize the pages in the admin panel 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 returntruefrom 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: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. Thedoctrine/dbal package is required to use this functionality:
--generate:
Note: If your table contains ENUM columns, doctrine/dbal is unable to scan your table and will crash. Hence Filament is unable to generate the schema for your resource if it contains an ENUM column. Read more about this issue here.
Handling soft deletes
By default, you will not be able to interact with deleted records in the admin panel. 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:
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:
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:
You may specify the name of an Eloquent accessor if just one column is inadequate at identifying a record.
Forms
Resource classes contain a staticform() method that is used to build the forms on the Create and Edit pages:
Fields
Theschema() method is used to define the structure of your form. It is an array of fields, in the order they should appear in your form.
We have many fields available for your forms, including:
- Text input
- Select
- Multi-select
- Checkbox
- Date-time picker
- File upload
- Rich editor
- Markdown editor
- Repeater
Layout
Form layouts are completely customizable. We have many layout components available, which can be used in any combination: To view a full list of available layout components, see the Form Builder documentation. You may also build your own completely custom layout components.Hiding components contextually
ThehiddenOn() method of form components allows you to dynamically hide fields based on the current page or action.
In this example, we hide the password field on the edit page:
visibleOn() shortcut method for only showing a field on one page or action:
Tables
Resource classes contain a statictable() method that is used to build the table on the List page:
Relations
Filament has many utilities available for managing resource relationships. Which solution you choose to use depends on your use case:BelongsTo
Select field
Filament includes the ability to automatically load options from aBelongsTo relationship:
Layout component
Layout form components are able to save child data to relationships, such asBelongsTo:
HasOne
Layout component
Layout form components are able to save child data to relationships, such asHasOne:
HasMany
Relation manager
“Relation managers” in Filament allow admins to list, create, associate, edit, dissociate and delete related records without leaving the resource’s Edit page. The related records are listed in a table, which has buttons to open a modal for each action. For more information on relation managers, see the full documentation.Repeater
Alternatively, if you’re looking to edit the relationship from the main form, you could use a repeater:HasManyThrough
Relation manager
“Relation managers” in Filament allow admins to list, create, edit, and delete related records without leaving the resource’s Edit page. The related records are listed in a table, which has buttons to open a modal for each action. For more information on relation managers, see the full documentation.BelongsToMany
Multi-select field
Filament can automatically loadSelect options from a BelongsToMany relationship:
Checkbox list field
Filament can automatically loadCheckboxList options from a BelongsToMany relationship:
CheckboxList is available in the Form docs.
Relation manager
“Relation managers” in Filament allow admins to list, create, attach, edit, detach and delete related records without leaving the resource’s Edit page. The related records are listed in a table, which has buttons to open a modal for each action. For more information on relation managers, see the full documentation.MorphTo
Select field
Filament includes the ability to automatically load options from aMorphTo relationship:
MorphOne
Layout component
Layout form components are able to save child data to relationships, such asMorphOne:
MorphMany
Relation manager
“Relation managers” in Filament allow admins to list, create, associate, edit, dissociate and delete related records without leaving the resource’s Edit page. The related records are listed in a table, which has buttons to open a modal for each action. For more information on relation managers, see the full documentation.Repeater
Alternatively, if you’re looking to edit the relationship from the main form, you could use a repeater:MorphToMany
Relation manager
“Relation managers” in Filament allow admins to list, create, attach, edit, detach and delete related records without leaving the resource’s Edit page. The related records are listed in a table, which has buttons to open a modal for each action. For more information on relation managers, see the full documentation.Authorization
For authorization, Filament will observe any model policies that are registered in your app. The following methods are used:viewAny()is used to completely hide resources from the navigation menu, and prevents the user from accessing any pages.create()is used to control creating new records.update()is used to control editing a record.view()is used to control viewing a record.delete()is used to prevent a single record from being deleted.deleteAny()is used to prevent records from being bulk deleted. Filament uses thedeleteAny()method because iterating through multiple records and checking thedelete()policy is not very performant.forceDelete()is used to prevent a single soft-deleted record from being force-deleted.forceDeleteAny()is used to prevent records from being bulk force-deleted. Filament uses theforceDeleteAny()method because iterating through multiple records and checking theforceDelete()policy is not very performant.restore()is used to prevent a single soft-deleted record from being restored.restoreAny()is used to prevent records from being bulk restored. Filament uses therestoreAny()method because iterating through multiple records and checking therestore()policy is not very performant.reorder()is used to control reordering a record.
Model labels
Each resource has a “model label” which is automatically generated from the model name. For example, anApp\Models\Customer model will have a customer label.
The label is used in several parts of the UI, and you may customize it using the $modelLabel property:
getModelLabel() to define a dynamic label:
Plural model label
Resources also have a “plural model label” which is automatically generated from the model label. For example, acustomer label will be pluralized into customers.
You may customize the plural version of the label using the $pluralModelLabel property:
getPluralModelLabel() method:
Navigation
Filament will automatically generate a navigation menu item for your resource using the plural label. If you’d like to customize the navigation item label, you may use the$navigationLabel property:
getNavigationLabel() method:
Icons
The$navigationIcon property supports the name of any Blade component. By default, the Blade Heroicons v1 package is installed, so you may use the name of any Heroicons v1 out of the box. However, you may create your own custom icon components or install an alternative library if you wish.
getNavigationIcon() method:
Sorting navigation items
The$navigationSort property allows you to specify the order in which navigation items are listed:
getNavigationSort() method:
Grouping navigation items
You may group navigation items by specifying a$navigationGroup property:
getNavigationGroup() method to set a dynamic group label:
Customizing the Eloquent query
Within Filament, every query to your resource model will start with thegetEloquentQuery() method.
Because of this, it’s very easy to apply your own model scopes that affect the entire resource. You can use this to implement multi-tenancy easily within the admin panel.
Disabling global scopes
By default, Filament will observe all global scopes that are registered to your model. However, this may not be ideal if you wish to access, for example, soft deleted records. To overcome this, you may override thegetEloquentQuery() method that Filament uses:
Customizing the URL slug
By default, Filament will generate a resource URL based on the name of the model. You can customize this by setting the$slug property on the resource:
Multi-tenancy
Multi-tenancy, simply, is the concept of users “owning” records, and only being able to access the records that they own within Filament.Simple multi-tenancy from scratch
Simple multi-tenancy is easy to set up with Filament. First, scope the base Eloquent query for every “owned” resource by defining thegetEloquentQuery() method:
whereBelongsTo() to scope the records to only those that belong to the currently authenticated user. However, you may use whatever Eloquent method you wish here, including a manual where() clause, or a scope.
Finally, you need to ensure that records are attached to the current user when they are first created. The easiest way to do this is through a model observer:
AttachAction or AssociateAction:
stancl/tenancy
To set up stancl/tenancy to work with Filament, you just need to add the InitializeTenancyByDomain::class middleware to Livewire and the Filament config file:
Deleting pages
If you’d like to delete a page from your resource, you can just delete the page file from thePages directory of your resource, and its entry in the getPages() method.
For example, you may have a resource with records that may not be created by anyone. Delete the Create page file, and then remove it from getPages():