Tricks

Passing parent resource to related CRUD pages

Feb 20, 2023
Nate Daly
Admin panel

There are occasions when you want to do more than the built in CRUD routing provided by Filament.

For this example you might want to create a Modules and Course builder and need the module ID in the create course page.

// CourseModuleResource
public static function getPages(): array
{
return [
'index' => ListModules::route('/'),
'create' => CreateModule::route('/create'),
'edit' => EditModule::route('/{record}/edit'),
'modules' => ListModules::route('/{record}'),
// Adding a new route for the create page passing in the ID of the parent record
'modules.create' => CreateModule::route('/{record}/create'),
];
}
// CourseModuleResource List Page
class ListModules extends ListRecords
{
protected static string $resource = CourseModuleResource::class;
 
protected function getActions(): array
{
// Get last segment which is the ID of the resource.
$record = request()?->segment(count(request()?->segments()));
 
return [
Actions\CreateAction::make()->url(fn (): string => url('admin/courses/modules/' . $record . '/create')),
];
}
}

No comments yet…