Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions packages/forms/docs/12-repeater.md
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,28 @@ Repeater::make('qualifications')

<UtilityInjection set="formFields" version="4.x" extras="Data;;array<string, mixed>;;$data;;The data that is being saved by the repeater.">You can inject various utilities into the function passed to `mutateRelationshipDataBeforeSaveUsing()` as parameters.</UtilityInjection>

### Running code after creating a related item

You may run code after a new related item is created in the database using the `afterCreateUsing()` method. This method accepts a closure that receives the current item's data in a `$data` variable and the newly created record in a `$record` variable. This is useful when you need the record's ID to perform additional operations, such as attaching pivot data:

```php
use Filament\Forms\Components\Repeater;
use Illuminate\Database\Eloquent\Model;

Repeater::make('variants')
->relationship()
->schema([
// ...
])
->afterCreateUsing(function (array $data, Model $record): void {
if (isset($data['attributes'])) {
$record->attributes()->attach($data['attributes']);
}
})
```

<UtilityInjection set="formFields" version="5.x" extras="Data;;array<string, mixed>;;$data;;The data that was used to create the record.||Record;;Illuminate\Database\Eloquent\Model;;$record;;The newly created record.">You can inject various utilities into the function passed to `afterCreateUsing()` as parameters.</UtilityInjection>

### Modifying related records after retrieval

You may filter or modify the related records of a repeater after they are retrieved from the database using the `modifyRecordsUsing` argument. This method accepts a function that receives a `Collection` of related records. You should return the modified collection.
Expand Down
27 changes: 27 additions & 0 deletions packages/forms/src/Components/Repeater.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ class Repeater extends Field implements CanConcealComponents, HasExtraItemAction

protected ?Closure $mutateRelationshipDataBeforeSaveUsing = null;

protected ?Closure $afterCreateUsing = null;

/**
* @var array<string, mixed> | null
*/
Expand Down Expand Up @@ -1003,6 +1005,7 @@ public function relationship(string | Closure | null $name = null, ?Closure $mod
}

$record = $relationship->save($record);
$component->afterCreate($itemData, $record);
$item->model($record)->saveRelationships();
$existingRecords->push($record);
}
Expand Down Expand Up @@ -1353,6 +1356,30 @@ public function mutateRelationshipDataBeforeSave(array $data, Model $record): ?a
return $data;
}

public function afterCreateUsing(?Closure $callback): static
{
$this->afterCreateUsing = $callback;

return $this;
}

public function afterCreate(array $data, Model $record): void
{
if ($this->afterCreateUsing instanceof Closure) {
$this->evaluate(
$this->afterCreateUsing,
namedInjections: [
'data' => $data,
'record' => $record,
],
typedInjections: [
Model::class => $record,
$record::class => $record,
],
);
}
}

public function canConcealComponents(): bool
{
return $this->isCollapsible();
Expand Down