Getting started
“Relation managers” in Filament allow administrators to list, create, attach, associate, edit, detach, dissociate and delete related records without leaving the resource’s Edit page. Resource classes contain a staticgetRelations() method that is used to register relation managers for your resource.
To create a relation manager, you can use the make:filament-relation-manager command:
CategoryResourceis the name of the resource class for the parent model.postsis the name of the relationship you want to manage.titleis the name of the attribute that will be used to identify posts.
CategoryResource/RelationManagers/PostsRelationManager.php file. This contains a class where you are able to define a form and table for your relation manager:
getRelations() method:
$inverseRelationship property on the relation manager:
Handling soft deletes
By default, you will not be able to interact with deleted records in the relation manager. If you’d like to add functionality to restore, force delete and filter trashed records in your relation manager, use the--soft-deletes flag when generating the relation manager:
Listing records
Related records will be listed in a table. The entire relation manager is based around this table, which contains actions to create, edit, attach / detach, associate / dissociate, and delete records. As per the documentation on listing records, you may use all the same utilities for customization on the relation manager: Additionally, you may use any other feature of the table builder.Listing with pivot attributes
ForBelongsToMany and MorphToMany relationships, you may also add pivot table attributes. For example, if you have a TeamsRelationManager for your UserResource, and you want to add the role pivot attribute to the table, you can use:
withPivot() method of the relationship and inverse relationship.
Creating records
Creating with pivot attributes
ForBelongsToMany and MorphToMany relationships, you may also add pivot table attributes. For example, if you have a TeamsRelationManager for your UserResource, and you want to add the role pivot attribute to the create form, you can use:
withPivot() method of the relationship and inverse relationship.
Customizing data before saving
Sometimes, you may wish to modify form data before it is finally saved to the database. To do this, you may use themutateFormDataUsing() method, which accepts the $data as an array, and returns the modified version:
Customizing the creation process
You can tweak how the record is created using theusing() method:
Customizing the save notification
When the record is successfully created, a notification is dispatched to the user, which indicates the success of their action. To customize the text content of this notification:Lifecycle hooks
Hooks may be used to execute code at various points within an action’s lifecycle.Halting the creation process
At any time, you may call$action->halt() from inside a lifecycle hook or mutation method, which will halt the entire creation process:
cancel() the action instead of halting it:
Editing records
Editing with pivot attributes
ForBelongsToMany and MorphToMany relationships, you may also edit pivot table attributes. For example, if you have a TeamsRelationManager for your UserResource, and you want to add the role pivot attribute to the edit form, you can use:
withPivot() method of the relationship and inverse relationship.
Customizing data before filling the form
You may wish to modify the data from a record before it is filled into the form. To do this, you may use themutateRecordDataUsing() method to modify the $data array, and return the modified version before it is filled into the form:
Customizing data before saving
Sometimes, you may wish to modify form data before it is finally saved to the database. To do this, you may define amutateFormDataUsing() method, which accepts the $data as an array, and returns it modified:
Customizing the saving process
You can tweak how the record is updated using theusing() method:
Customizing the save notification
When the record is successfully updated, a notification is dispatched to the user, which indicates the success of their action. To customize the text content of this notification:Lifecycle hooks
Hooks may be used to execute code at various points within an action’s lifecycle.Halting the saving process
At any time, you may call$action->halt() from inside a lifecycle hook or mutation method, which will halt the entire saving process:
cancel() the action instead of halting it:
Attaching and detaching records
Filament is able to attach and detach records forBelongsToMany and MorphToMany relationships.
When generating your relation manager, you may pass the --attach flag to also add AttachAction, DetachAction and DetachBulkAction to the table:
$table arrays:
Preloading the attachment modal select options
By default, as you search for a record to attach, options will load from the database via AJAX. If you wish to preload these options when the form is first loaded instead, you can use thepreloadRecordSelect() method of AttachAction:
Attaching with pivot attributes
When you attach record with theAttach button, you may wish to define a custom form to add pivot attributes to the relationship:
$action->getRecordSelect() outputs the select field to pick the record to attach. The role text input is then saved to the pivot table’s role column.
Please ensure that any pivot attributes are listed in the withPivot() method of the relationship and inverse relationship.
Scoping the options
You may want to scope the options available toAttachAction:
Handling duplicates
By default, you will not be allowed to attach a record more than once. This is because you must also set up a primaryid column on the pivot table for this feature to work.
Please ensure that the id attribute is listed in the withPivot() method of the relationship and inverse relationship.
Finally, add the $allowsDuplicates property to the relation manager:
Associating and dissociating records
Filament is able to associate and dissociate records forHasMany and MorphMany relationships.
When generating your relation manager, you may pass the --associate flag to also add AssociateAction, DissociateAction and DissociateBulkAction to the table:
$table arrays:
Preloading the associate modal select options
By default, as you search for a record to associate, options will load from the database via AJAX. If you wish to preload these options when the form is first loaded instead, you can use thepreloadRecordSelect() method of AssociateAction:
Scoping the options
You may want to scope the options available toAssociateAction:
Viewing records
When generating your relation manager, you may pass the--view flag to also add a ViewAction to the table:
ViewAction to the $table->actions() array:
Deleting records
By default, you will not be able to interact with deleted records in the relation manager. If you’d like to add functionality to restore, force delete and filter trashed records in your relation manager, use the--soft-deletes flag when generating the relation manager:
Lifecycle hooks
You can use thebefore() and after() methods to execute code before and after a record is deleted:
Halting the deletion process
At any time, you may call$action->halt() from inside a lifecycle hook or mutation method, which will halt the entire deletion process:
cancel() the action instead of halting it:
Accessing the owner record
Relation managers are Livewire components. When they are first loaded, the owner record (the Eloquent record which serves as a parent - the main resource model) is mounted in a public$ownerRecord property. Thus, you may access the owner record using:
static method like form() or table(), $this isn’t accessible. So, you may use a callback to access the $livewire instance:
$livewire->ownerRecord in.
Grouping relation managers
You may choose to group relation managers together into one tab. To do this, you may wrap multiple managers in aRelationGroup object, with a label:
Conditional visibility
By default, relation managers will be visible if theviewAny() method for the related model policy returns true.
You may use the canViewForRecord() method to determine if the relation manager should be visible for a specific owner record:
Moving the resource form to tabs
On the Edit or View page class, override thehasCombinedRelationManagerTabsWithForm() method: