Normalize labor category sort_order + fix increment step to 10

Migration renumbers all categories per company as 10, 20, 30… to
eliminate existing duplicates. Observer now increments by 10 instead
of 1 to leave headroom between positions.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-09-08 06:53:10 +00:00
parent 04cab25ac5
commit 2e082d4aad
2 changed files with 29 additions and 1 deletions
+1 -1
View File
@@ -35,7 +35,7 @@ class LaborCategory extends Model
->where('company_id', $cat->company_id)
->where('sort_order', '>=', $new)
->where('id', '!=', $cat->id ?? 0)
->increment('sort_order');
->increment('sort_order', 10);
}
});
}
@@ -0,0 +1,28 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
return new class extends Migration
{
public function up(): void
{
$companies = DB::table('companies')->pluck('id');
foreach ($companies as $companyId) {
$rows = DB::table('labor_categories')
->where('company_id', $companyId)
->orderBy('sort_order')
->orderBy('id')
->pluck('id');
foreach ($rows as $i => $id) {
DB::table('labor_categories')
->where('id', $id)
->update(['sort_order' => ($i + 1) * 10]);
}
}
}
public function down(): void {}
};