54 lines
1.2 KiB
PHP
54 lines
1.2 KiB
PHP
<?php
|
|||
|
|
|
||
|
|
namespace App\Console\Commands;
|
||
|
|
|
||
|
|
use App\Models\User;
|
||
|
|
use Illuminate\Console\Command;
|
||
|
|
|
||
|
|
class GenerateUserSlugs extends Command
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* The name and signature of the console command.
|
||
|
|
*
|
||
|
|
* @var string
|
||
|
|
*/
|
||
|
|
protected $signature = 'users:generate-slugs';
|
||
|
|
|
||
|
|
/**
|
||
|
|
* The console command description.
|
||
|
|
*
|
||
|
|
* @var string
|
||
|
|
*/
|
||
|
|
protected $description = 'Generate unique slugs for all users without slugs';
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Execute the console command.
|
||
|
|
*/
|
||
|
|
public function handle()
|
||
|
|
{
|
||
|
|
$this->info('Generating slugs for users...');
|
||
|
|
|
||
|
|
$usersWithoutSlugs = User::whereNull('slug')->get();
|
||
|
|
|
||
|
|
if ($usersWithoutSlugs->isEmpty()) {
|
||
|
|
$this->info('All users already have slugs!');
|
||
|
|
return Command::SUCCESS;
|
||
|
|
}
|
||
|
|
|
||
|
|
$bar = $this->output->createProgressBar($usersWithoutSlugs->count());
|
||
|
|
$bar->start();
|
||
|
|
|
||
|
|
foreach ($usersWithoutSlugs as $user) {
|
||
|
|
$user->slug = User::generateUniqueSlug($user->name);
|
||
|
|
$user->save();
|
||
|
|
$bar->advance();
|
||
|
|
}
|
||
|
|
|
||
|
|
$bar->finish();
|
||
|
|
$this->newLine();
|
||
|
|
$this->info("Successfully generated slugs for {$usersWithoutSlugs->count()} users!");
|
||
|
|
|
||
|
|
return Command::SUCCESS;
|
||
|
|
}
|
||
|
|
}
|