© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
FilamentF
Filament•2y ago•
4 replies
dwiser

Action button for creating relation on view page

I'm trying to duplicate the CreateAction button that is created with a RelationManager so that I can add it to the top of a ViewRecord page.

I got myself most of the way there with associating a model and reusing the Resource form but I'm not sure how to pass the record of the ViewRecord page to the form and also hide the select element in the form that pertains to the record being viewed.

// Resources\ProjectResource\Pages\ViewProject->getHeaderActions():
        return [
            ...
            Actions\CreateAction::make()
                ->model(Shift::class)
                ->label('Add Shift')
                ->form(fn (Forms\Form $form) => ShiftResource::form($form)),
            ...
        ];

// Resources\ShiftResource::form(Forms\Form $form):
        return $form
            ->schema([
                ...
                Forms\Components\Select::make('project_uuid')
                    ->required()
                    ->relationship(name: 'project', titleAttribute: 'name')
                    ->searchable()
                    ->preload()
                    ->optionsLimit(20)
                    ->columnSpan([
                        'sm' => 2,
                        'xl' => 3,
                    ])
                    ->hiddenOn(ProjectShiftsRelationManager::class),
                ...
            ]);
// Resources\ProjectResource\Pages\ViewProject->getHeaderActions():
        return [
            ...
            Actions\CreateAction::make()
                ->model(Shift::class)
                ->label('Add Shift')
                ->form(fn (Forms\Form $form) => ShiftResource::form($form)),
            ...
        ];

// Resources\ShiftResource::form(Forms\Form $form):
        return $form
            ->schema([
                ...
                Forms\Components\Select::make('project_uuid')
                    ->required()
                    ->relationship(name: 'project', titleAttribute: 'name')
                    ->searchable()
                    ->preload()
                    ->optionsLimit(20)
                    ->columnSpan([
                        'sm' => 2,
                        'xl' => 3,
                    ])
                    ->hiddenOn(ProjectShiftsRelationManager::class),
                ...
            ]);
Solution
In my case, that doesn't work because I'm not in the context of a form or resource.
Filament\Resources\Pages\ViewRecord
Filament\Resources\Pages\ViewRecord
is being passed through and doesn't have an $ownerRecord property.

However, that led to me searching through methods on the ViewRecord page and I found the
->getRecord()
->getRecord()
method which gets the model for me and can be used in the current context.

Working code for my situation is here:
// in App\Filament\App\Resources\ProjectResource\Pages\ViewProject
    protected function getHeaderActions(): array
    {
        return [
            Actions\CreateAction::make()
                ->model(Shift::class)
                ->fillForm(fn (): array => ['project_uuid' => $this->getRecord()->uuid] )
                ->form(fn (Forms\Form $form) => ShiftResource::form($form)), // Reusing an existing for schema.
        ];
    }
// in App\Filament\App\Resources\ProjectResource\Pages\ViewProject
    protected function getHeaderActions(): array
    {
        return [
            Actions\CreateAction::make()
                ->model(Shift::class)
                ->fillForm(fn (): array => ['project_uuid' => $this->getRecord()->uuid] )
                ->form(fn (Forms\Form $form) => ShiftResource::form($form)), // Reusing an existing for schema.
        ];
    }

This will give me a button on the view page of a record where I can click to create a new relationship and prefill the ID (uuid) of the current resource.
Jump to solution
Filament banner
FilamentJoin
A powerful open source UI framework for Laravel • Build and ship admin panels & apps fast with Livewire
20,307Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Relation manager only on view page
FilamentFFilament / ❓┊help
2y ago
add action on infolist/view on relation manager?
FilamentFFilament / ❓┊help
2y ago
Add Edit action on View page
FilamentFFilament / ❓┊help
2y ago
Conditional action on view resource page
FilamentFFilament / ❓┊help
3y ago