Filament Nested Resources allows you to create nested resources of any depth. This is useful for resources which are too complex for relation manager, yet don't make sense as a standalone resource.
Your support is key to the continual advancement of our plugin. We appreciate every user who has contributed to our journey so far.
While our plugin is available for all to use, if you are utilizing it for commercial purposes and believe it adds significant value to your business, we kindly ask you to consider supporting us through GitHub Sponsors. This sponsorship will assist us in continuous development and maintenance to keep our plugin robust and up-to-date. Any amount you contribute will greatly help towards reaching our goals. Join us in making this plugin even better and driving further innovation.
You can install the package via composer:
composer require guava/filament-nested-resources
Throughout the documentation I refer to root
nested resource and child
nested resources. The only difference is that the Root
is the first resource in the relationship tree.
In the examples: ArtistResource > AlbumResource > SongResource
Artist would be the root resource, the other would be child resources.
Currently we support one-to-many and polymoprhic one-to-many relationships only.
To better understand how nested resources work and to troubleshoot any issues you might encounter, we've created a demo laravel project: https://github.com/GuavaCZ/filament-nested-resources-demo
In order to set up Nested Resources, you need to do these steps:
NestedResource
trait. You will be required to implement the getAncestor
method. For the root resource, return null
, for all child resources implement it according to the documentation below.NestedPage
trait.RelationManager
(Filament documentation) or a RelationPage
(Filament documentation) to bind the Resources together. Add the NestedRelationManager
trait to either of them.Let's imagine the scenario from the Showcase screenshots, where we have this schema:
First we create ArtistResource
:
use Filament\Resources\Resource;use Guava\FilamentNestedResources\Concerns\NestedResource; class ArtistResource extends Resource{ use NestedResource; // If using Relation Manager: public static function getRelations(): array { return [ AlbumsRelationManager::class, ]; } public static function getPages(): array { return [ 'index' => Pages\ListArtists::route('/'), 'create' => Pages\CreateArtist::route('/create'), 'edit' => Pages\EditArtist::route('/{record}/edit'), 'view' => Pages\ViewArtist::route('/{record}'), // In case of relation page. // Make sure the name corresponds to the name of your actual relationship on the model. 'albums' => Pages\ManageArtistAlbums::route('/{record}/albums'), // Needed to create child records // The name should be '{relationshipName}.create': 'albums.create' => Pages\CreateArtistAlbum::route('/{record}/albums/create'), ]; } public static function getAncestor(): ?Ancestor { return null; }}
Next, we create the AlbumResource
:
use Filament\Resources\Resource;use Guava\FilamentNestedResources\Concerns\NestedResource;Â class AlbumResource extends Resource{ use NestedResource;Â public static function getRelations(): array { return [ // Repeat the same for Song Resource ]; }Â public static function getAncestor() : ?Ancestor { // Configure the ancestor (parent) relationship here return Ancestor::make( 'albums', // Relationship name 'artist', // Inverse relationship name ); }}
In every page of our ArtistResource
and AlbumResource
, we add the NestedPage
trait:
use Filament\Resources\Pages\CreateRecord;use Guava\FilamentNestedResources\Concerns\NestedPage;Â class CreateArtist extends CreateRecord{ use NestedPage;Â //}
use Filament\Resources\Pages\EditRecord;use Guava\FilamentNestedResources\Concerns\NestedPage;Â class EditArtist extends EditRecord{ use NestedPage;Â //}
use Filament\Resources\Pages\ListRecords;use Guava\FilamentNestedResources\Concerns\NestedPage;Â class ListArtists extends ListRecords{ use NestedPage;Â //}
Now let`s create a new page which will be used to create child records. Let's create CreateArtistAlbum
page inside ArtistResource/Pages
:
use Guava\FilamentNestedResources\Pages\CreateRelatedRecord;use Guava\FilamentNestedResources\Concerns\NestedPage;Â class CreateArtistAlbum extends CreateRelatedRecord{ use NestedPage;Â // This page also needs to know the ancestor relationship used (just like relation managers): protected static string $relationship = 'albums';Â // We can usually guess the nested resource, but if your app has multiple resources for this // model, you will need to explicitly define it // public static string $nestedResource = AlbumResource::class;}
Don`t forget to register the page in the getPages
method.
And finally we create either the AlbumsRelationManager
or if you prefer to use a Relation Page then create a ManageArtistAlbums
page. We just need to add the NestedRelationManager
trait here.
For RelationManager:
use Filament\Resources\RelationManagers\RelationManager;use Guava\FilamentNestedResources\Concerns\NestedRelationManager;Â class AlbumsRelationManager extends RelationManager{ use NestedRelationManager;Â // We can usually guess the nested resource, but if your app has multiple resources for this // model, you will need to explicitly define the it // public static string $nestedResource = AlbumResource::class;}
For RelationPage:
use Filament\Resources\Pages\ManageRelatedRecords;use Guava\FilamentNestedResources\Concerns\NestedPage;use Guava\FilamentNestedResources\Concerns\NestedRelationManager;Â class ManageArtistAlbums extends ManageRelatedRecords{ use NestedPage; // Since this is a standalone page, we also need this trait use NestedRelationManager;Â //}
Optionally, we recommend deleting the index
and create
pages from your child NestedResources
(in this case AlbumResource).
Every resource can control their own part of the breadcrumb, which by default consists of an index
breadcrumb and a detail
breadcrumb.
The index
breadcrumb typically redirects to the index page.
The detail
breadcrumb typically redirects to the detail (edit or view) and by default, will display the route key (ID if not configured otherwise) of the record.
You can override the label via the getBreadcrumbRecordLabel
method of a NestedResource
:
public static function getBreadcrumbRecordLabel(Model $record){ return $record->first_name . ' ' . $record->last_name;}
Or you can override the resource`s breadcrumb part completely, by overriding the getBreadcrumbs
method on the resource:
public static function getBreadcrumbs(Model $record, string $operation): array{ return [ 'my-custom-url' => 'My custom label',];}
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.