Self-Referencing Nested Resource

I'm failing to set up a Resource where Pages can have subpages (of the same class). Is that somehow possible using a Nested Resources ? I've set up a parent and children relationship on the Page-Model. Following the docs I've managed to display child records but so far without a nested breadcrumb as the resource can't reference itself as $parentResource. Am I missing something? What would be the Filament way to do this?
3 Replies
mmoollllee
mmoolllleeOP2mo ago
What works:
namespace App\Filament\Resources\Pages;

class PageResource extends Resource
{
protected static ?string $model = Page::class;

public static function getRelations(): array
{
return [
'children' => SubPagesManager::class
];
}

public static function getEloquentQuery(): Builder
{
return parent::getEloquentQuery()
->whereNull('parent_id');
}
}
namespace App\Filament\Resources\Pages;

class PageResource extends Resource
{
protected static ?string $model = Page::class;

public static function getRelations(): array
{
return [
'children' => SubPagesManager::class
];
}

public static function getEloquentQuery(): Builder
{
return parent::getEloquentQuery()
->whereNull('parent_id');
}
}
namespace App\Filament\Resources\Pages\Resources\Subpages;

class SubpageResource extends Resource
{
public static function getParentResourceRegistration(): ?ParentResourceRegistration
{
return PageResource::asParent()
->relationship('children') // name of the hasMany on Page
->inverseRelationship('parent'); // name of the belongsTo on Page
}
}
namespace App\Filament\Resources\Pages\Resources\Subpages;

class SubpageResource extends Resource
{
public static function getParentResourceRegistration(): ?ParentResourceRegistration
{
return PageResource::asParent()
->relationship('children') // name of the hasMany on Page
->inverseRelationship('parent'); // name of the belongsTo on Page
}
}
namespace App\Filament\Resources\Pages\RelationManagers;

class SubPagesManager extends RelationManager
{
protected static string $relationship = 'children';

protected static ?string $relatedResource = SubpageResource::class;
}
namespace App\Filament\Resources\Pages\RelationManagers;

class SubPagesManager extends RelationManager
{
protected static string $relationship = 'children';

protected static ?string $relatedResource = SubpageResource::class;
}
Is there a way to reuse the "PageResource" as "SubPageResource"? I'm just wondering if theres a easier/better/less complex way...
LeandroFerreira
LeandroFerreira2mo ago
sorry, I didn't understand the question. Isn't it a nested resource?
mmoollllee
mmoolllleeOP2mo ago
Currently I need a SubpageResource and a PageResource. I'm wondering if theres a more elegant way by just having a PageResource with some kind of recursive behaviour by reusing the class itself as parent...

Did you find this page helpful?