feat(category): implement category configuration management with sorting and validation
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
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::table('categorias', function (Blueprint $table): void {
|
||||
$table->unsignedInteger('sort_order')->default(0);
|
||||
$table->index(['tenant_code', 'categoria_id', 'sort_order']);
|
||||
});
|
||||
|
||||
// Preserve the alphabetical navigation order until an administrator reorders it.
|
||||
$orders = [];
|
||||
foreach (DB::table('categorias')->orderBy('nombre')->orderBy('id')->get() as $category) {
|
||||
$group = json_encode([$category->tenant_code, $category->categoria_id]);
|
||||
$order = $orders[$group] ?? 0;
|
||||
DB::table('categorias')->where('id', $category->id)->update(['sort_order' => $order]);
|
||||
$orders[$group] = $order + 1;
|
||||
}
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('categorias', function (Blueprint $table): void {
|
||||
$table->dropIndex(['tenant_code', 'categoria_id', 'sort_order']);
|
||||
$table->dropColumn('sort_order');
|
||||
});
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user