Restructure batch origins: nested entries per ingredient with Supplier/Farmer selects
Deploy / deploy (push) Successful in 14s
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:
1 parent
400237aa5b
commit
4a98beeb45
6 files changed
+221
-89
No files matched your search
@@ -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');
|
||||
}
|
||||
};
|
||||
Reference in new issue
Block a user