Create multiple records at once (pseudo code)

I have a form with 3 fields. 1 field is inside a repeater and the other 2 are not, as they are common to all records. If the user enters 10 repeaters, 10 records will be persisted. What I did to make it work is customize the creation process inside my create page class:
protected function handleRecordCreation(array $data): Model
{
// pseudo code
foreach (myrepeater as repeater)
{
$models[] = static::getModel()::create($repeater_and_common_data)
}

// just to be compliance with the return type
return $models[0];
}
protected function handleRecordCreation(array $data): Model
{
// pseudo code
foreach (myrepeater as repeater)
{
$models[] = static::getModel()::create($repeater_and_common_data)
}

// just to be compliance with the return type
return $models[0];
}
My question is: Is this the way to go or does this code smell bad?
3 Replies
Babiute🍎🐍
Babiute🍎🐍6mo ago
Someone?
awcodes
awcodes6mo ago
Is this on a custom page? I mean it’s valid to do it this way, but it doesn’t make sense to use a repeater to create records of the same model. Typically a repeater would be for storing array/json data on a record or records of different models via a relationship.
Babiute🍎🐍
Babiute🍎🐍6mo ago
Hey @awcodes Thx for the answer. It is not a custom page. It's a CreateResouce page, but with some customization (meybe would be better to do it in a custom page)? The business rule is: The person can make a Request (this is the model) for a single item. To make life easier for the user, I was asked to create a page where the user could indicate the different items and create 1 request for each item (hence the repeater). There are a few other fields that will be common (for example, the destination and a description). These are handled outside the repeater and used to create each Request.