Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

DNABeast's avatar

Editing all children of a resource at the same time.

I've got Page resource and each Page model hasMany SubPages. It's easy to setup Filament to allow me to edit each subpage in a modal or a slideout, but I'd like to put all of the attached Subpages underneath the Page form and update them all then save them all at the same time. (Obviously this is a bad idea for many hundreds of children but in this instance I expect only a handful of subpages)

When I set up the form for the Page it knows my resource and attachs things like the TextInput::make('title'), but is there a way to set these up for the children of the page's resource?

I guess what I want is something like...

Section::make('subpages')
	->relationship('subpages')
	->schema([
		TextInput::make('title'),
		RichEditor::make('content')
	]),

...and have it iterate through each one.

I'm thinking I might need to make a custom component but again I don't know how to set each child as the resource to attach schema fields to.

0 likes
2 replies
LaryAI's avatar
Level 58

To achieve the functionality you're looking for in Filament, you can use the HasManyRepeater field to edit all the SubPages related to a Page at the same time. This field allows you to define a schema that will be repeated for each related model. Here's how you can set it up:

use Filament\Forms\Components\HasManyRepeater;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\RichEditor;

// Inside your Page resource form schema definition:

HasManyRepeater::make('subpages')
    ->relationship('subpages') // Ensure 'subpages' is the correct relationship method name on your Page model
    ->schema([
        TextInput::make('title')
            ->required(), // Add any validation rules or other properties as needed
        RichEditor::make('content')
            ->required(), // Add any validation rules or other properties as needed
    ])
    ->createItemButtonLabel('Add Subpage') // Customize the button label if you want
    ->dehydrated() // This will save the related models when the form is saved
    ->collapsed() // This will start with the repeater fields collapsed
    ->collapsible(), // This will allow each repeater field to be collapsed

When you use HasManyRepeater, Filament will automatically handle the iteration over each related SubPage and render the defined schema for each one. When the form is saved, it will also handle saving the related models.

Make sure that your Page model has the correct relationship method defined for subpages. For example:

class Page extends Model
{
    // ...

    public function subpages()
    {
        return $this->hasMany(SubPage::class);
    }

    // ...
}

Also, ensure that your SubPage model has the appropriate fillable or guarded properties set to allow mass assignment of the title and content fields.

Remember to handle validation and any other business logic that might be necessary when updating the SubPages. If you have custom logic that needs to be executed when saving the SubPages, you might need to override the save method on the form or use form hooks provided by Filament to integrate your logic.

DNABeast's avatar

Lary got kind of close.

Repeater::make('parts')
		->relationship('subpage')
		->schema([
			TextInput::make('title'),
		]),

Please or to participate in this conversation.