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()
->action(null)
->url(InvoiceResource::getUrl('create', ['ownerRecord' => $this->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!