2021-03-08 23:34:22 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
use Illuminate\Database\Migrations\Migration;
|
|
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
|
|
use Illuminate\Support\Facades\Schema;
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
|
|
|
class AddUserSlug extends Migration
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Run the migrations.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function up()
|
|
|
|
{
|
|
|
|
Schema::table('users', function (Blueprint $table) {
|
2021-04-27 21:53:22 +02:00
|
|
|
$table->string('slug', 180);
|
2021-03-08 23:34:22 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
$slugMap = [];
|
|
|
|
DB::table('users')->cursor()->each(function ($user) use (&$slugMap) {
|
|
|
|
$userSlug = Str::slug($user->name);
|
|
|
|
while (isset($slugMap[$userSlug])) {
|
|
|
|
$userSlug = Str::slug($user->name . Str::random(4));
|
|
|
|
}
|
|
|
|
$slugMap[$userSlug] = true;
|
|
|
|
|
|
|
|
DB::table('users')
|
|
|
|
->where('id', $user->id)
|
|
|
|
->update(['slug' => $userSlug]);
|
|
|
|
});
|
|
|
|
|
|
|
|
Schema::table('users', function (Blueprint $table) {
|
|
|
|
$table->unique('slug');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reverse the migrations.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function down()
|
|
|
|
{
|
|
|
|
Schema::table('users', function (Blueprint $table) {
|
|
|
|
$table->dropColumn('slug');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|