ibrostudio / filament-dynamic-resource-children
Allows Filament plugins developer to register pages or relation managers in a resource living at the app level or in an another plugin
Requires
- php: ^8.1
- filament/filament: ^2.12
- illuminate/contracts: ^9.0
- spatie/laravel-package-tools: ^1.9.2
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.8
- ibrostudio/filament-plugin-tools: ^1.0
- nunomaduro/collision: ^6.0
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^7.0
- pestphp/pest: ^1.21
- pestphp/pest-plugin-laravel: ^1.1
- pestphp/pest-plugin-mock: ^1.0
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.5
- spatie/laravel-ray: ^1.26
This package is auto-updated.
Last update: 2024-11-09 04:51:43 UTC
README
This package allows Filament plugins developer to register pages or relation managers in a resource living at the app level or in an another plugin.
Installation
You can install the package via composer:
composer require ibrostudio/filament-dynamic-resource-children
Usage
Prepare your resource
Add the trait CanHaveDynamicChildren
to your resource class.
use IBroStudio\FilamentDynamicResourceChildren\Concerns\HasDynamicChildren; class ExampleParentResource extends Resource { use HasDynamicChildren; }
Then, in that resource class, modify getRelations()
and getPages()
methods:
Before:
public static function getRelations(): array { return []; } public static function getPages(): array { return [ 'index' => Pages\ListExampleParents::route('/'), 'create' => Pages\CreateExampleParent::route('/create'), 'view' => Pages\ViewExampleParent::route('/{record}'), 'edit' => Pages\EditExampleParent::route('/{record}/edit'), ]; }
After:
public static function getRelations(): array { return array_merge( [], self::getDynamicRelationManagers() ); } public static function getPages(): array { return array_merge( [ 'index' => Pages\ListExampleParents::route('/'), 'create' => Pages\CreateExampleParent::route('/create'), 'view' => Pages\ViewExampleParent::route('/{record}'), 'edit' => Pages\EditExampleParent::route('/{record}/edit'), ], self::getDynamicPages() ); }
Link your children with their parent
Create your page or relation manager as usual in your plugin.
Then register it in your plugin service provider like this:
use App\Filament\Resources\ExampleParentResource; use Filament\PluginServiceProvider; use Spatie\LaravelPackageTools\Package; use VendorName\MyPlugin\Filament\Resources\ExampleParentResource\Pages\MyFirstPage; use VendorName\MyPlugin\Filament\Resources\ExampleParentResource\Pages\MySecondPage; use VendorName\MyPlugin\Filament\Resources\ExampleParentResource\RelationManagers\MyFirstRelationManager; use VendorName\MyPlugin\Filament\Resources\ExampleParentResource\RelationManagers\MySecondRelationManager; class MyPluginServiceProvider extends PluginServiceProvider { protected array $pages = [ MyFirstPage::class, MySecondPage::class, ]; protected array $relationManagers = [ MyFirstRelationManager::class, MySecondRelationManager::class, ]; public function configurePackage(Package $package): void { $this->packageConfiguring($package); $package ->name('my-plugin') ->hasViews(); } public function packageRegistered(): void { parent::packageRegistered(); $this->app->resolving('filament', function () { ExampleParentResource::addDynamicPages([ 'first-page-key' => [ 'class' => MyFirstPage::class, 'route' => '/first-page-slug' ], 'second-page-key' => [ 'class' => MySecondPage::class, 'route' => '/second-page-slug' ], ]); ExampleParentResource::addDynamicRelationManagers([ MyFirstRelationManager::class, MySecondRelationManager::class, ]); }); } }
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
- iBroStudio
- The Filament team, thanks to them for their work
License
The MIT License (MIT). Please see License File for more information.