parent
ec5d3ea669
commit
6cf75a6439
@ -0,0 +1,69 @@
|
|||||||
|
<?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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Loading…
Reference in new issue