public function mergeMany(array $entities, array $data, array $options = [])
{
$indexed = (new Collection($data))->groupBy('id')->map(function ($element, $key) {
return $key === '' ? $element : $element[0];
})->toArray();
$new = isset($indexed[null]) ? $indexed[null] : [];
unset($indexed[null]);
$output = [];
foreach ($entities as $record) {
if (!$record instanceof EntityInterface) {
continue;
}
$id = $record->id;
if (!isset($indexed[$id])) {
continue;
}
$output[] = $this->merge($record, $indexed[$id], $options);
unset($indexed[$id]);
}
$new = array_merge($indexed, $new);
foreach ($new as $newRecord) {
$output[] = $this->one($newRecord, $options);
}
return $output;
}