guava / filament-nested-resources
Fund package maintenance!
GuavaCZ
Installs: 51 983
Dependents: 1
Suggesters: 0
Security: 0
Stars: 105
Watchers: 2
Forks: 17
Open Issues: 8
Requires
- filament/filament: ^3.0.0
- livewire/livewire: ^3.0
- spatie/laravel-package-tools: ^1.15
Requires (Dev)
- filament/spatie-laravel-translatable-plugin: ^3.2
- laravel/pint: ^1.11
- orchestra/testbench: ^8.0
- dev-main
- dev-beta
- dev-alpha
- 1.x-dev
- 1.2.0
- 1.1.0
- 1.0.1
- 1.0.0
- 1.0.0-beta.5
- 1.0.0-beta.4
- 1.0.0-beta.3
- 1.0.0-beta.2
- 1.0.0-beta.1
- 1.0.0-alpha.11
- 1.0.0-alpha.10
- 1.0.0-alpha.9
- 1.0.0-alpha.8
- 1.0.0-alpha.7
- 1.0.0-alpha.6
- 1.0.0-alpha.5
- 1.0.0-alpha.4
- 1.0.0-alpha.3
- 1.0.0-alpha.2
- 1.0.0-alpha.1
- dev-fix/page-actions
- dev-fix/camel-case-helpers
- dev-fix/route-names
- dev-develop
This package is auto-updated.
Last update: 2024-10-27 07:29:39 UTC
README
Filament Nested Resources
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.
First public release is here.
Showcase
Support us
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.
Installation
You can install the package via composer:
composer require guava/filament-nested-resources
Usage
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.
Supported relationships
Currently we support one-to-many and polymoprhic one-to-many relationships only.
Demo Project
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
Quick start
In order to set up Nested Resources, you need to do these steps:
- On the resources (root and all child resources) you want to nest, add the
NestedResource
trait. You will be required to implement thegetAncestor
method. For the root resource, returnnull
, for all child resources implement it according to the documentation below. - On every page of the resources from the 1st step, add the
NestedPage
trait. - Create a
RelationManager
(Filament documentation) or aRelationPage
(Filament documentation) to bind the Resources together. Add theNestedRelationManager
trait to either of them.
Let's imagine the scenario from the Showcase screenshots, where we have this schema:
- Artist Model.
- Album Model (Belongs to Artist).
- Song Model (Belongs to Album).
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).
Customizing the breadcrumbs
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', ]; }
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.