Resources
Nested resources
Overview
Relation managers and relation pages provide you with an easy way to render a table of related records inside a resource.
For example, in a CourseResource
, you may have a relation manager or page for lessons
that belong to that course. You can create and edit lessons from the table, which opens modal dialogs.
However, lessons may be too complex to be created and edited in a modal. You may wish that lessons had their own resource, so that creating and editing them would be a full page experience. This is a nested resource.
Creating a nested resource
To create a nested resource, you can use the make:filament-resource
command with the --nested
option:
php artisan make:filament-resource Lesson --nested
To access the nested resource, you will also need a relation manager or relation page. This is where the user can see the list of related records, and click links to the “create” and “edit” pages.
To create a relation manager or page, you can use the make:filament-relation-manager
or make:filament-page
command:
php artisan make:filament-relation-manager CourseResource lessons title
php artisan make:filament-page ManageCourseLessons --resource=CourseResource --type=ManageRelatedRecords
When creating a relation manager or page, Filament will ask if you want each table row to link to a resource instead of opening a modal, to which you should answer “yes” and select the nested resource that you just created.
After generating the relation manager or page, it will have a property pointing to the nested resource:
use App\Filament\Resources\Courses\Resources\Lessons\LessonResource;
protected static ?string $relatedResource = LessonResource::class;
The nested resource class will have a property pointing to the parent resource:
use App\Filament\Resources\Courses\CourseResource;
protected static ?string $parentResource = CourseResource::class;
Customizing the relationship names
In the same way that relation managers and pages predict the name of relationships based on the models in those relationships, nested resources do the same. Sometimes, you may have a relationship that does not fit the traditional relationship naming convention, and you will need to inform Filament of the correct relationship names for the nested resource.
To customize the relationship names, first remove the $parentResource
property from the nested resource class. Then define a getParentResourceRegistration()
method:
use App\Filament\Resources\Courses\CourseResource;
use Filament\Resources\ParentResourceRegistration;
public static function getParentResourceRegistration(): ?ParentResourceRegistration
{
return CourseResource::asParent()
->relationship('lessons')
->inverseRelationship('course');
}
You can omit the calls to relationship()
and inverseRelationship()
if you want to use the default names.
Still need help? Join our Discord community or open a GitHub discussion