Replaces the recipe dropdown with an inline Section containing a name field and a spice-repeater with percentage inputs. On save, the recipe is created (version 1) or updated in place; spice pivot rows are synced via sync(). SumEquals100 rule enforces that percentages add up to 100. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
af55857883
commit
4775cffaae
3 files changed
+100
-48
No files matched your search
@@ -3,19 +3,20 @@
|
||||
namespace App\Filament\User\Resources\ProductResource\Pages;
|
||||
|
||||
use App\Filament\User\Resources\ProductResource;
|
||||
use App\Models\ProductSize;
|
||||
use App\Models\Recipe;
|
||||
use Filament\Resources\Pages\CreateRecord;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use App\Models\ProductSize;
|
||||
|
||||
class CreateProduct extends CreateRecord
|
||||
{
|
||||
protected static string $resource = ProductResource::class;
|
||||
|
||||
protected function handleRecordCreation(array $data): Model {
|
||||
|
||||
protected function handleRecordCreation(array $data): Model
|
||||
{
|
||||
$record = static::getModel()::create($data);
|
||||
|
||||
foreach ($data['sizes'] as $size) {
|
||||
foreach ($data['sizes'] ?? [] as $size) {
|
||||
ProductSize::create([
|
||||
'product_id' => $record->id,
|
||||
'packing_id' => $size['packing_id'],
|
||||
@@ -25,6 +26,25 @@ class CreateProduct extends CreateRecord
|
||||
'gtin' => $size['gtin'] ?? null,
|
||||
]);
|
||||
}
|
||||
|
||||
$recipeName = $data['recipe_name'] ?? null;
|
||||
$spices = $data['recipe_spices'] ?? [];
|
||||
|
||||
if ($recipeName) {
|
||||
$recipe = Recipe::create([
|
||||
'name' => $recipeName,
|
||||
'product_id' => $record->id,
|
||||
'version' => 1,
|
||||
]);
|
||||
|
||||
$pivotData = collect($spices)->mapWithKeys(fn ($s) => [
|
||||
$s['spice_id'] => ['amount_in_percent' => $s['amount_in_percent']],
|
||||
])->toArray();
|
||||
|
||||
$recipe->spices()->sync($pivotData);
|
||||
$record->update(['recipes_id' => $recipe->id]);
|
||||
}
|
||||
|
||||
return $record;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user