DeathSummon
DeathSummon
FFilament
Created by DeathSummon on 3/11/2025 in #❓┊help
Filament Repeater: How to Create Multiple Records from a Single Entry?
I'm running into an issue with Filament’s repeater field and could use some help. My Use Case: I have a repeater with two fields in each item: one field lets the user select an item (e.g., "Item A") and another field lets them specify a quantity (e.g., 3). My goal is that a single repeater item should ultimately create multiple related records. For example, if a user selects "Item A" with quantity 3, I want to create an array like this:
[
['item' => 'A'],
['item' => 'A'],
['item' => 'A'],
]
[
['item' => 'A'],
['item' => 'A'],
['item' => 'A'],
]
My Code:
Repeater::make('cartonTransactions')
->relationship() // Relationship is set up correctly
->schema([
Select::make('item')
->label('Item')
->options(Item::pluck('name', 'id'))
->required(),
TextInput::make('quantity')
->numeric()
->minValue(1)
->required(),
])
->mutateRelationshipDataBeforeCreateUsing(function (array $data): array {
// I want to create multiple records based on the quantity field.
$quantity = (int) $data['quantity'];
$item = $data['item'];

$records = [];
for ($i = 0; $i < $quantity; $i++) {
$records[] = [
'item' => $item,
];
}
// Returning an array of arrays instead of a single associative array.
return $records;
});
Repeater::make('cartonTransactions')
->relationship() // Relationship is set up correctly
->schema([
Select::make('item')
->label('Item')
->options(Item::pluck('name', 'id'))
->required(),
TextInput::make('quantity')
->numeric()
->minValue(1)
->required(),
])
->mutateRelationshipDataBeforeCreateUsing(function (array $data): array {
// I want to create multiple records based on the quantity field.
$quantity = (int) $data['quantity'];
$item = $data['item'];

$records = [];
for ($i = 0; $i < $quantity; $i++) {
$records[] = [
'item' => $item,
];
}
// Returning an array of arrays instead of a single associative array.
return $records;
});
The Problem: Filament’s callback is designed to return a single associative array per repeater item. Because I'm returning an array of arrays, only the first record is being processed. This results in an error when saving the relationship. The Error I Received: SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed How can I resolve this?
1 replies