mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-06-05 09:34:37 +08:00
@ -12,9 +12,11 @@
|
||||
*/
|
||||
|
||||
$factory->define(\BookStack\Auth\User::class, function ($faker) {
|
||||
$name = $faker->name;
|
||||
return [
|
||||
'name' => $faker->name,
|
||||
'name' => $name,
|
||||
'email' => $faker->email,
|
||||
'slug' => \Illuminate\Support\Str::slug($name . '-' . \Illuminate\Support\Str::random(5)),
|
||||
'password' => Str::random(10),
|
||||
'remember_token' => Str::random(10),
|
||||
'email_confirmed' => 1
|
||||
|
50
database/migrations/2021_03_08_215138_add_user_slug.php
Normal file
50
database/migrations/2021_03_08_215138_add_user_slug.php
Normal file
@ -0,0 +1,50 @@
|
||||
<?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) {
|
||||
$table->string('slug', 250);
|
||||
});
|
||||
|
||||
$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');
|
||||
});
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user