To achieve the desired functionality in Laravel Filament, you can use the Relation Manager to manage the relationships between your models. However, since you want to manage all three models from one page and show all forms in modals, you will need to set up nested Relation Managers.
Here's a step-by-step guide on how you can set up your Filament page to manage Categories, Elements, and Contents:
-
Create a Filament Resource for each of your models (Category, Element, Content).
-
In your
CategoryResource, set up a Relation Manager for Elements. This will allow you to manage Elements within a Category.
use Filament\Resources\Form;
use Filament\Resources\Table;
use Filament\Resources\RelationManagers\HasManyRelationManager;
class CategoryResource extends Resource
{
// ...
public static function form(Form $form)
{
return $form
// Define fields for Category
// ...
;
}
public static function table(Table $table)
{
return $table
// Define columns for Category
// ...
;
}
public static function getRelations()
{
return [
ElementsRelationManager::class,
];
}
public static function getPages()
{
return [
// Define pages for Category
// ...
];
}
}
class ElementsRelationManager extends HasManyRelationManager
{
protected static string $relationship = 'elements';
protected function getTableQuery()
{
return parent::getTableQuery()
// Optionally, add constraints or modify the query
;
}
protected function getTableColumns()
{
return [
// Define columns for Elements within Category
// ...
];
}
protected function getFormSchema()
{
return [
// Define fields for Elements within Category
// ...
];
}
}
- Similarly, in your
ElementResource, set up a Relation Manager for Contents.
class ElementResource extends Resource
{
// ...
public static function getRelations()
{
return [
ContentsRelationManager::class,
];
}
// ...
}
class ContentsRelationManager extends HasManyRelationManager
{
protected static string $relationship = 'contents';
// Define getTableColumns, getFormSchema, etc. for Contents
}
- Now, to manage Contents within Elements within Categories, you can use the
ElementsRelationManagerwithinCategoryResourceto include a nested Relation Manager for Contents.
class ElementsRelationManager extends HasManyRelationManager
{
// ...
protected function getTableColumns()
{
return [
// Define columns for Elements
// ...
// Add a nested relation manager for Contents
Tables\Columns\TextColumn::make('contents_count')
->counts('contents')
->url(fn ($record): string => URL::route('filament.resources.elements.edit', [
'record' => $record,
'relation' => 'contents',
])),
];
}
}
- Finally, ensure that your
CategoryResourceandElementResourcepages are set up to handle the nested relations and that the forms and tables are displayed in modals as desired.
This setup allows you to manage Categories, Elements, and Contents from one page, with each level of the hierarchy being managed through nested Relation Managers. The ElementsRelationManager within CategoryResource will provide a link to manage the related Contents, which will be handled by the ContentsRelationManager within ElementResource.
Remember to adjust the getTableColumns, getFormSchema, and other methods to match the fields and relationships of your actual models.