- 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>
This commit is contained in:
1 parent
811f5a015e
commit
3301a1e9b2
3 files changed
+55
-9
No files matched your search
@@ -5,6 +5,7 @@ namespace App\Filament\User\Resources\ProductResource\Pages;
|
|||||||
use App\Filament\User\Resources\ProductResource;
|
use App\Filament\User\Resources\ProductResource;
|
||||||
use App\Models\ProductSize;
|
use App\Models\ProductSize;
|
||||||
use App\Models\Recipe;
|
use App\Models\Recipe;
|
||||||
|
use Filament\Notifications\Notification;
|
||||||
use Filament\Resources\Pages\CreateRecord;
|
use Filament\Resources\Pages\CreateRecord;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
@@ -14,36 +15,50 @@ class CreateProduct extends CreateRecord
|
|||||||
|
|
||||||
protected function handleRecordCreation(array $data): Model
|
protected function handleRecordCreation(array $data): Model
|
||||||
{
|
{
|
||||||
|
$sizes = $data['sizes'] ?? [];
|
||||||
|
$spices = $this->data['recipe_spices'] ?? [];
|
||||||
|
unset($data['sizes']);
|
||||||
|
|
||||||
|
try {
|
||||||
$record = static::getModel()::create($data);
|
$record = static::getModel()::create($data);
|
||||||
|
|
||||||
foreach ($data['sizes'] ?? [] as $size) {
|
foreach ($sizes as $size) {
|
||||||
|
if (empty($size['packing_id'])) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$gtin = preg_replace('/\D/', '', (string) ($size['gtin'] ?? ''));
|
||||||
ProductSize::create([
|
ProductSize::create([
|
||||||
'product_id' => $record->id,
|
'product_id' => $record->id,
|
||||||
'packing_id' => $size['packing_id'],
|
'packing_id' => (int) $size['packing_id'],
|
||||||
'description' => $size['description'] ?? null,
|
'description' => $size['description'] ?? null,
|
||||||
'price' => $size['price'],
|
'price' => $size['price'],
|
||||||
'sku' => $size['sku'] ?? null,
|
'sku' => $size['sku'] ?: null,
|
||||||
'gtin' => $size['gtin'] ?? null,
|
'gtin' => $gtin !== '' ? $gtin : null,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
$spices = $data['recipe_spices'] ?? [];
|
|
||||||
|
|
||||||
if (! empty($spices)) {
|
if (! empty($spices)) {
|
||||||
$recipe = Recipe::create([
|
$recipe = Recipe::create([
|
||||||
'name' => $data['title'],
|
'name' => $data['title'],
|
||||||
'product_id' => $record->id,
|
'product_id' => $record->id,
|
||||||
'version' => 1,
|
'version' => 1,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$pivotData = collect($spices)->mapWithKeys(fn ($s) => [
|
$pivotData = collect($spices)->mapWithKeys(fn ($s) => [
|
||||||
$s['spice_id'] => ['amount_in_percent' => $s['amount_in_percent']],
|
(int) $s['spice_id'] => ['amount_in_percent' => (float) $s['amount_in_percent']],
|
||||||
])->toArray();
|
])->toArray();
|
||||||
|
|
||||||
$recipe->spices()->sync($pivotData);
|
$recipe->spices()->sync($pivotData);
|
||||||
$record->update(['recipes_id' => $recipe->id]);
|
$record->update(['recipes_id' => $recipe->id]);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $record;
|
return $record;
|
||||||
|
} catch (\Throwable $e) {
|
||||||
|
Notification::make()
|
||||||
|
->title('Fehler beim Anlegen')
|
||||||
|
->body($e->getMessage())
|
||||||
|
->danger()
|
||||||
|
->persistent()
|
||||||
|
->send();
|
||||||
|
throw $e;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -51,8 +51,17 @@ class EditProduct extends EditRecord
|
|||||||
|
|
||||||
protected function afterSave(): void
|
protected function afterSave(): void
|
||||||
{
|
{
|
||||||
|
try {
|
||||||
$this->syncSizes();
|
$this->syncSizes();
|
||||||
$this->syncRecipe();
|
$this->syncRecipe();
|
||||||
|
} catch (\Throwable $e) {
|
||||||
|
Notification::make()
|
||||||
|
->title('Fehler beim Speichern')
|
||||||
|
->body($e->getMessage())
|
||||||
|
->danger()
|
||||||
|
->persistent()
|
||||||
|
->send();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function syncSizes(): void
|
protected function syncSizes(): void
|
||||||
|
|||||||
@@ -0,0 +1,22 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::table('recipes_spices', function (Blueprint $table) {
|
||||||
|
$table->decimal('amount_in_percent', 6, 2)->change();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::table('recipes_spices', function (Blueprint $table) {
|
||||||
|
$table->decimal('amount_in_percent', 4, 2)->change();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
Reference in new issue
Block a user