diff --git a/app/Http/Controllers/StudentsController.php b/app/Http/Controllers/StudentsController.php index 264945e2..94a1ff8c 100644 --- a/app/Http/Controllers/StudentsController.php +++ b/app/Http/Controllers/StudentsController.php @@ -13,6 +13,7 @@ use Illuminate\Support\Collection; use Illuminate\Support\Facades\Auth; use Illuminate\View\View; +use Illuminate\Support\Str; class StudentsController extends Controller { @@ -71,7 +72,7 @@ public function store(Request $request): Student|false $user = $request->user(); $student = $user->studentProfiles()->create([ - 'slug' => $user->pea, + 'slug' => Str::ulid(), 'full_name' => $user->display_name, 'first_name' => $user->firstname, 'last_name' => $user->lastname, diff --git a/database/migrations/2025_03_31_163648_rename_slug_to_pea_in_students_table.php b/database/migrations/2025_03_31_163648_rename_slug_to_pea_in_students_table.php new file mode 100644 index 00000000..7e3f4e69 --- /dev/null +++ b/database/migrations/2025_03_31_163648_rename_slug_to_pea_in_students_table.php @@ -0,0 +1,28 @@ +renameColumn('slug', 'pea'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('students', function (Blueprint $table) { + $table->renameColumn('pea', 'slug'); + }); + } +}; diff --git a/database/migrations/2025_03_31_164643_add_slug_to_students_table.php b/database/migrations/2025_03_31_164643_add_slug_to_students_table.php new file mode 100644 index 00000000..53bd2e56 --- /dev/null +++ b/database/migrations/2025_03_31_164643_add_slug_to_students_table.php @@ -0,0 +1,28 @@ +ulid('slug')->nullable()->after('pea'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('students', function (Blueprint $table) { + $table->dropColumn('slug'); + }); + } +}; diff --git a/database/migrations/2025_04_01_150237_make_slug_not_nullable_in_students_table.php b/database/migrations/2025_04_01_150237_make_slug_not_nullable_in_students_table.php new file mode 100644 index 00000000..99437841 --- /dev/null +++ b/database/migrations/2025_04_01_150237_make_slug_not_nullable_in_students_table.php @@ -0,0 +1,34 @@ + 'v2_1_Seeder', + '--force' => true + ]); + + Schema::table('students', function (Blueprint $table) { + $table->ulid('slug')->nullable(false)->change(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('students', function (Blueprint $table) { + $table->ulid('slug')->nullable()->change(); + }); + } +}; diff --git a/database/seeders/v2_1_Seeder.php b/database/seeders/v2_1_Seeder.php new file mode 100644 index 00000000..a0aa2c4c --- /dev/null +++ b/database/seeders/v2_1_Seeder.php @@ -0,0 +1,38 @@ +whereNull('slug') + ->orderBy('id') + ->chunkById(200, function ($students) { + $this->command->withProgressBar($students, fn($student) => $this->updateStudent($student)); + $this->command->newLine(); + }); + } + + /** + * Replaces a student slug with a unique ULID + */ + public function updateStudent(Student $student): void + { + $existing_student = DB::table('students')->where('id', $student->id) ?? null; + + if ($existing_student) { + $existing_student->update(['slug' => Str::ulid()]); + } + } +}