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:
@@ -35,7 +35,7 @@ class LaborCategory extends Model
|
|||||||
->where('company_id', $cat->company_id)
|
->where('company_id', $cat->company_id)
|
||||||
->where('sort_order', '>=', $new)
|
->where('sort_order', '>=', $new)
|
||||||
->where('id', '!=', $cat->id ?? 0)
|
->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 {}
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user