Black Friday: coupon FRIDAY24 for 40% off Yearly/Lifetime membership! Read more here

Filament Form with Tabs: Add Relation Managers as Tabs

2024-11-21

You can merge relation managers with a form. But if your form has tabs, you can merge relation managers into a form tab. Let me show you how.


First, a basic form with a tab layout.

public static function form(Form $form): Form
{
return $form
->schema([
Tabs::make('Tabs')
->columnSpanFull()
->tabs([
Tabs\Tab::make('Customer')
->schema([
Forms\Components\TextInput::make('name')
->columnSpanFull(),
Forms\Components\TextInput::make('email')
->email()
->columnSpanFull(),
]),
]),
])->inlineLabel();
}

Now, we have one tab for the customer information.

Next, we can add a relation manager to every other tab. To do that, we can insert a Livewire component into the form. As a Livewire component, we will call the relation manager.

When inserting a Relation Manager, we must pass ownerRecord and pageClass as parameters.

public static function form(Form $form): Form
{
return $form
->schema([
Tabs::make('Tabs')
->columnSpanFull()
->tabs([
Tabs\Tab::make('Customer')
->schema([
Forms\Components\TextInput::make('name')
->columnSpanFull(),
Forms\Components\TextInput::make('email')
->email()
->columnSpanFull(),
]),
Tabs\Tab::make('Orders')
->schema([
Forms\Components\Livewire::make(RelationManagers\OrdersRelationManager::class, fn (Page $livewire, Customer $record) => [
'ownerRecord' => $record,
'pageClass' => $livewire::class,
]),
]),
]),
])->inlineLabel();
}

And that's it; we can see a relation manager in the tab.

With this approach, you will have a problem if your resource can create records. A simple fix is to add visibleOn('edit') for every tab.

Tabs\Tab::make('Orders')
->visibleOn('edit')
->schema([
Forms\Components\Livewire::make(RelationManagers\OrdersRelationManager::class, fn (Page $livewire, Customer $record) => [
'ownerRecord' => $record,
'pageClass' => $livewire::class,
]),
]),

The original solution is from Leandro Ferreira on Discord.

A few of our Premium Examples: