Files
Malek TellissiandClaude Sonnet 4.6 3301a1e9b2
Deploy / deploy (push) Successful in 14s
Fix 500 on recipe save and surface errors as notifications
- Fix root cause: recipes_spices.amount_in_percent was decimal(4,2) which
  overflows on 100% — widened to decimal(6,2) via migration
- Wrap afterSave() in try-catch so DB/logic errors show as a persistent
  danger notification instead of a blank 500 page
- Same for CreateProduct: errors show as notification; also reads
  recipe_spices from raw Livewire $this->data (not $data param which
  excludes dehydrated(false) fields) so recipes actually save on create

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-09-12 18:19:31 +02:00

65 lines
2.2 KiB
PHP

<?php
namespace App\Filament\User\Resources\ProductResource\Pages;
use App\Filament\User\Resources\ProductResource;
use App\Models\ProductSize;
use App\Models\Recipe;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\CreateRecord;
use Illuminate\Database\Eloquent\Model;
class CreateProduct extends CreateRecord
{
protected static string $resource = ProductResource::class;
protected function handleRecordCreation(array $data): Model
{
$sizes = $data['sizes'] ?? [];
$spices = $this->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;
}
}
}