You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
70 lines
1.8 KiB
70 lines
1.8 KiB
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Models\User;
|
|
use Illuminate\Console\Command;
|
|
|
|
class TrimUserName extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'trim:username';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = '去除用户名字字段中的空格';
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*
|
|
* @return int
|
|
*/
|
|
public function handle()
|
|
{
|
|
$this->info('开始处理用户名字空格...');
|
|
|
|
// 查找名字中包含空格的用户
|
|
$users = User::where('name', 'like', '% %')
|
|
->orWhere('username', 'like', '% %')
|
|
->get();
|
|
|
|
$count = 0;
|
|
foreach ($users as $user) {
|
|
$updated = false;
|
|
|
|
// 处理 name 字段
|
|
if ($user->name && str_contains($user->name, ' ')) {
|
|
$oldName = $user->name;
|
|
$user->name = str_replace(' ', '', $user->name);
|
|
$this->line("用户ID: {$user->id}, name: '{$oldName}' => '{$user->name}'");
|
|
$updated = true;
|
|
}
|
|
|
|
// 处理 username 字段
|
|
if ($user->username && str_contains($user->username, ' ')) {
|
|
$oldUsername = $user->username;
|
|
$user->username = str_replace(' ', '', $user->username);
|
|
$this->line("用户ID: {$user->id}, username: '{$oldUsername}' => '{$user->username}'");
|
|
$updated = true;
|
|
}
|
|
|
|
if ($updated) {
|
|
$user->save();
|
|
$count++;
|
|
}
|
|
}
|
|
|
|
$this->info("处理完成,共更新 {$count} 个用户");
|
|
|
|
return self::SUCCESS;
|
|
}
|
|
}
|
|
|