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.