Tricks

Relation Manager create record using Resource page instead of modal

Jun 2, 2022
Blackpig
Admin panel

The default behaviour when hitting the Create button on the Relation Manager is for the new record form to be displayed in a modal.

Sometimes that related record may be a little big or cumbersome to be handled in a modal and it may be quicker or simpler to redirect and create the record using the Resource's Create page.

Let's say we have a Customer Resource with an InvoiceRelationManager.

First we need to use the CreateAction() on the InvoiceRelationManager

use Filament\Tables\Actions\CreateAction;
 
CreateAction::make()
->url(fn ($livewire) => InvoiceResource::getUrl('create', ['ownerRecord' => $livewire->ownerRecord->getKey()]))

Note that we pass in the calling customer's record key as a query param to our URL ['ownerRecord' => $this->ownerRecord->getKey()]

We can now pre-populate the Customer BelongsToSelect on the InvoiceResource

Forms\Components\BelongsToSelect::make('customer_id')
->default(request()->query('ownerRecord'))

Et voila!

avatar

Nice 👌

avatar

I got "Using $this when not in object context"

any idea? thanks

avatar

btw, another workaround is get the key from url request()->segment(3)

avatar

BelongsToSelect is deprecated right now the same sample code using relationship()

Forms\Components\Select::make('customer_id') ->relationship('custumer', 'name') ->default(request()->query('ownerRecord'))

avatar

We detected that request()->query('ownerRecord') returns null after selecting the action create & create another.

avatar

Hi, after reactive(), request()->query('ownerRecord') return Null, how to solve this problem.

avatar

Hi Audruis! Our solution is to look for the query parameter ownerRecord in the previus url. url()->previous();

avatar

Can we do without relation manager ? After open child records, on listing page it shows button "New Record" would like to add param when create new record

eg. from location listing page navigating to branch page. it automatically filter location using params but New branch url can not be modified Location -> Branch

public static function getPages(): array
{
return [
'create' => Pages\CreatePlan::route('/create',['location_id' => request()->query('tableFilters[location][value]')]),
->url(fn ($livewire) => self::getUrl('create', ['location_id' => request()->query('tableFilters[location][value]')])),
];
}
```