Restructure batch origins: nested entries per ingredient with Supplier/Farmer selects
Deploy / deploy (push) Successful in 14s

Each BatchOrigin (spice) now has multiple BatchOriginEntry rows, replacing the flat
single-supplier/farmer fields. Lieferant and Landwirt are Select fields backed by
dedicated tables with inline create support. Migration preserves existing data.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Malek TellissiandClaude Sonnet 4.6 committed 2026-09-12 20:03:05 +02:00
1 parent 400237aa5b
commit 4a98beeb45
6 files changed
+221 -89

No files matched your search

+44 -31
View File
@@ -4,6 +4,7 @@ namespace App\Filament\User\Resources;
use App\Filament\User\Resources\BatchResource\Pages; use App\Filament\User\Resources\BatchResource\Pages;
use App\Models\Batch; use App\Models\Batch;
use App\Models\Farmer;
use App\Models\Product; use App\Models\Product;
use App\Models\ProductSize; use App\Models\ProductSize;
use App\Models\Recipe; use App\Models\Recipe;
@@ -56,11 +57,8 @@ class BatchResource extends Resource
$recipe = Recipe::with('spices')->find($product->recipes_id); $recipe = Recipe::with('spices')->find($product->recipes_id);
if ($recipe) { if ($recipe) {
$set('origins', $recipe->spices->map(fn ($s) => [ $set('origins', $recipe->spices->map(fn ($s) => [
'spice_id' => $s->id, 'spice_id' => $s->id,
'supplier_id' => null, 'entries' => [['supplier_id' => null, 'origin_country' => null, 'farmer_id' => null, 'notes' => null]],
'origin_country' => null,
'farmer' => null,
'notes' => null,
])->toArray()); ])->toArray());
} }
} }
@@ -93,11 +91,8 @@ class BatchResource extends Resource
$recipe = Recipe::with('spices')->find($state); $recipe = Recipe::with('spices')->find($state);
if ($recipe) { if ($recipe) {
$set('origins', $recipe->spices->map(fn ($s) => [ $set('origins', $recipe->spices->map(fn ($s) => [
'spice_id' => $s->id, 'spice_id' => $s->id,
'supplier_id' => null, 'entries' => [['supplier_id' => null, 'origin_country' => null, 'farmer_id' => null, 'notes' => null]],
'origin_country' => null,
'farmer' => null,
'notes' => null,
])->toArray()); ])->toArray());
} }
}), }),
@@ -133,34 +128,52 @@ class BatchResource extends Resource
->label('') ->label('')
->relationship() ->relationship()
->schema([ ->schema([
Forms\Components\Select::make('spice_id') Forms\Components\Hidden::make('spice_id'),
Forms\Components\Placeholder::make('spice_name')
->label('Rohstoff') ->label('Rohstoff')
->options(Spice::orderBy('name')->pluck('name', 'id')) ->content(fn (Get $get): string =>
->searchable() Spice::find($get('spice_id'))?->name ?? '—'
->required(), )
->columnSpanFull(),
Forms\Components\Select::make('supplier_id') Forms\Components\Repeater::make('entries')
->label('Lieferant') ->label('Herkunftsangaben')
->relationship('supplier', 'name') ->relationship()
->searchable() ->schema([
->preload() Forms\Components\Select::make('supplier_id')
->nullable(), ->label('Lieferant')
->options(fn () => \App\Models\Supplier::orderBy('name')->pluck('name', 'id'))
->searchable()
->createOptionForm([
Forms\Components\TextInput::make('name')->label('Name')->required(),
])
->createOptionUsing(fn ($data) => \App\Models\Supplier::create($data)->id)
->nullable(),
Forms\Components\TextInput::make('origin_country') Forms\Components\TextInput::make('origin_country')
->label('Herkunftsland') ->label('Herkunftsland')
->maxLength(100), ->maxLength(100),
Forms\Components\TextInput::make('farmer') Forms\Components\Select::make('farmer_id')
->label('Landwirt / Betrieb') ->label('Landwirt')
->maxLength(255), ->options(fn () => Farmer::orderBy('name')->pluck('name', 'id'))
->searchable()
->createOptionForm([
Forms\Components\TextInput::make('name')->label('Name')->required(),
])
->createOptionUsing(fn ($data) => Farmer::create($data)->id)
->nullable(),
Forms\Components\Textarea::make('notes') Forms\Components\Textarea::make('notes')
->label('Notizen') ->label('Notizen')
->rows(2) ->rows(2)
->columnSpanFull(),
])
->columns(3)
->addActionLabel('Eintrag hinzufügen')
->reorderable(false)
->columnSpanFull(), ->columnSpanFull(),
]) ])
->columns(2)
->addActionLabel('Eintrag hinzufügen')
->reorderable(false), ->reorderable(false),
]), ]),
]); ]);
@@ -2,6 +2,7 @@
namespace App\Filament\User\Resources\ProductResource\RelationManagers; namespace App\Filament\User\Resources\ProductResource\RelationManagers;
use App\Models\Farmer;
use App\Models\ProductSize; use App\Models\ProductSize;
use App\Models\Recipe; use App\Models\Recipe;
use App\Models\Spice; use App\Models\Spice;
@@ -59,11 +60,8 @@ class BatchesRelationManager extends RelationManager
return; return;
} }
$set('origins', $recipe->spices->map(fn ($s) => [ $set('origins', $recipe->spices->map(fn ($s) => [
'spice_id' => $s->id, 'spice_id' => $s->id,
'supplier_id' => null, 'entries' => [['supplier_id' => null, 'origin_country' => null, 'farmer_id' => null, 'notes' => null]],
'origin_country' => null,
'farmer' => null,
'notes' => null,
])->toArray()); ])->toArray());
}), }),
@@ -92,60 +90,76 @@ class BatchesRelationManager extends RelationManager
Forms\Components\DatePicker::make('expires_at') Forms\Components\DatePicker::make('expires_at')
->label('Mindesthaltbarkeitsdatum'), ->label('Mindesthaltbarkeitsdatum'),
Forms\Components\Repeater::make('origins') Forms\Components\Section::make('Herkunft der Rohstoffe')->schema([
->label('Herkunft der Rohstoffe') Forms\Components\Repeater::make('origins')
->relationship() ->label('')
->afterStateHydrated(function ($component, $record, $livewire) { ->relationship()
// Pre-fill from product's current recipe only when creating a new batch ->afterStateHydrated(function ($component, $record, $livewire) {
if ($record?->id) { if ($record?->id) {
return; return;
} }
$recipeId = $livewire->getOwnerRecord()->recipes_id; $recipeId = $livewire->getOwnerRecord()->recipes_id;
if (! $recipeId) { if (! $recipeId) {
return; return;
} }
$recipe = Recipe::with('spices')->find($recipeId); $recipe = Recipe::with('spices')->find($recipeId);
if (! $recipe) { if (! $recipe) {
return; return;
} }
$component->state($recipe->spices->map(fn ($s) => [ $component->state($recipe->spices->map(fn ($s) => [
'spice_id' => $s->id, 'spice_id' => $s->id,
'supplier_id' => null, 'entries' => [['supplier_id' => null, 'origin_country' => null, 'farmer_id' => null, 'notes' => null]],
'origin_country' => null, ])->toArray());
'farmer' => null, })
'notes' => null, ->schema([
])->toArray()); Forms\Components\Hidden::make('spice_id'),
}) Forms\Components\Placeholder::make('spice_name')
->schema([ ->label('Rohstoff')
Forms\Components\Select::make('spice_id') ->content(fn (Get $get): string =>
->label('Rohstoff') Spice::find($get('spice_id'))?->name ?? '—'
->options(Spice::orderBy('name')->pluck('name', 'id')) )
->searchable() ->columnSpanFull(),
->required(),
Forms\Components\Select::make('supplier_id') Forms\Components\Repeater::make('entries')
->label('Lieferant') ->label('Herkunftsangaben')
->relationship('supplier', 'name') ->relationship()
->searchable() ->schema([
->preload() Forms\Components\Select::make('supplier_id')
->nullable(), ->label('Lieferant')
->options(fn () => \App\Models\Supplier::orderBy('name')->pluck('name', 'id'))
->searchable()
->createOptionForm([
Forms\Components\TextInput::make('name')->label('Name')->required(),
])
->createOptionUsing(fn ($data) => \App\Models\Supplier::create($data)->id)
->nullable(),
Forms\Components\TextInput::make('origin_country') Forms\Components\TextInput::make('origin_country')
->label('Herkunftsland') ->label('Herkunftsland')
->maxLength(100), ->maxLength(100),
Forms\Components\TextInput::make('farmer') Forms\Components\Select::make('farmer_id')
->label('Landwirt / Betrieb') ->label('Landwirt')
->maxLength(255), ->options(fn () => Farmer::orderBy('name')->pluck('name', 'id'))
->searchable()
->createOptionForm([
Forms\Components\TextInput::make('name')->label('Name')->required(),
])
->createOptionUsing(fn ($data) => Farmer::create($data)->id)
->nullable(),
Forms\Components\Textarea::make('notes') Forms\Components\Textarea::make('notes')
->label('Notizen') ->label('Notizen')
->rows(2) ->rows(2)
->columnSpanFull(), ->columnSpanFull(),
]) ])
->columns(2) ->columns(3)
->addActionLabel('Eintrag hinzufügen') ->addActionLabel('Eintrag hinzufügen')
->columnSpanFull(), ->reorderable(false)
->columnSpanFull(),
])
->reorderable(false),
]),
]); ]);
} }
+3 -3
View File
@@ -6,7 +6,7 @@ use Illuminate\Database\Eloquent\Model;
class BatchOrigin extends Model class BatchOrigin extends Model
{ {
protected $fillable = ['batch_id', 'spice_id', 'supplier_id', 'origin_country', 'farmer', 'notes']; protected $fillable = ['batch_id', 'spice_id'];
public function batch() public function batch()
{ {
@@ -18,8 +18,8 @@ class BatchOrigin extends Model
return $this->belongsTo(Spice::class); return $this->belongsTo(Spice::class);
} }
public function supplier() public function entries()
{ {
return $this->belongsTo(Supplier::class); return $this->hasMany(BatchOriginEntry::class);
} }
} }
+25
View File
@@ -0,0 +1,25 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class BatchOriginEntry extends Model
{
protected $fillable = ['batch_origin_id', 'supplier_id', 'origin_country', 'farmer_id', 'notes'];
public function batchOrigin()
{
return $this->belongsTo(BatchOrigin::class);
}
public function supplier()
{
return $this->belongsTo(Supplier::class);
}
public function farmer()
{
return $this->belongsTo(Farmer::class);
}
}
+10
View File
@@ -0,0 +1,10 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Farmer extends Model
{
protected $fillable = ['name'];
}
@@ -0,0 +1,70 @@
<?php
use App\Models\Farmer;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('farmers', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
Schema::create('batch_origin_entries', function (Blueprint $table) {
$table->id();
$table->foreignId('batch_origin_id')->constrained('batch_origins')->cascadeOnDelete();
$table->foreignId('supplier_id')->nullable()->constrained('suppliers')->nullOnDelete();
$table->string('origin_country')->nullable();
$table->foreignId('farmer_id')->nullable()->constrained('farmers')->nullOnDelete();
$table->text('notes')->nullable();
$table->timestamps();
});
// Migrate existing batch_origin rows → batch_origin_entries
$existing = DB::table('batch_origins')->get();
foreach ($existing as $row) {
$farmerId = null;
if (! empty($row->farmer)) {
$farmerId = DB::table('farmers')->insertGetId([
'name' => $row->farmer,
'created_at' => now(),
'updated_at' => now(),
]);
}
DB::table('batch_origin_entries')->insert([
'batch_origin_id' => $row->id,
'supplier_id' => $row->supplier_id,
'origin_country' => $row->origin_country,
'farmer_id' => $farmerId,
'notes' => $row->notes,
'created_at' => now(),
'updated_at' => now(),
]);
}
Schema::table('batch_origins', function (Blueprint $table) {
$table->dropForeign(['supplier_id']);
$table->dropColumn(['supplier_id', 'origin_country', 'farmer', 'notes']);
});
}
public function down(): void
{
Schema::table('batch_origins', function (Blueprint $table) {
$table->foreignId('supplier_id')->nullable()->constrained('suppliers')->nullOnDelete();
$table->string('origin_country')->nullable();
$table->string('farmer')->nullable();
$table->text('notes')->nullable();
});
Schema::dropIfExists('batch_origin_entries');
Schema::dropIfExists('farmers');
}
};