data['recipe_spices'] ?? []; unset($data['sizes']); try { $record = static::getModel()::create($data); foreach ($sizes as $size) { if (empty($size['packing_id'])) { continue; } $gtin = preg_replace('/\D/', '', (string) ($size['gtin'] ?? '')); ProductSize::create([ 'product_id' => $record->id, 'packing_id' => (int) $size['packing_id'], 'description' => $size['description'] ?? null, 'price' => $size['price'], 'sku' => $size['sku'] ?: null, 'gtin' => $gtin !== '' ? $gtin : null, ]); } if (! empty($spices)) { $recipe = Recipe::create([ 'name' => $data['title'], 'product_id' => $record->id, 'version' => 1, ]); $pivotData = collect($spices)->mapWithKeys(fn ($s) => [ (int) $s['spice_id'] => ['amount_in_percent' => (float) $s['amount_in_percent']], ])->toArray(); $recipe->spices()->sync($pivotData); $record->update(['recipes_id' => $recipe->id]); } return $record; } catch (\Throwable $e) { Notification::make() ->title('Fehler beim Anlegen') ->body($e->getMessage()) ->danger() ->persistent() ->send(); throw $e; } } }