From 1294b4801cbe4db61866b5d62c506f07d9b386e6 Mon Sep 17 00:00:00 2001 From: zhaiyuxin103 Date: Sun, 29 Dec 2024 11:32:42 +0800 Subject: [PATCH] =?UTF-8?q?feat(users):=20=E6=95=B0=E6=8D=AE=E6=A8=A1?= =?UTF-8?q?=E5=9E=8B=E5=A2=9E=E5=8A=A0=20name=20=E5=92=8C=20alias=20?= =?UTF-8?q?=E5=AD=97=E6=AE=B5=20(#71)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _ide_helper_models.php | 6 ++- app/Console/Commands/UpdateUserNameAlias.php | 49 +++++++++++++++++++ app/Console/Commands/UpdateUserToAdmin.php | 2 +- app/Models/User.php | 16 +----- app/Observers/UserObserver.php | 6 +++ ...0843_add_name_and_alias_to_users_table.php | 31 ++++++++++++ 6 files changed, 93 insertions(+), 17 deletions(-) create mode 100644 app/Console/Commands/UpdateUserNameAlias.php create mode 100644 database/migrations/2024_12_29_030843_add_name_and_alias_to_users_table.php diff --git a/_ide_helper_models.php b/_ide_helper_models.php index 3a2a170..4335453 100644 --- a/_ide_helper_models.php +++ b/_ide_helper_models.php @@ -509,13 +509,13 @@ class IdeHelperTerm {} * @property \Illuminate\Support\Carbon|null $created_at * @property \Illuminate\Support\Carbon|null $updated_at * @property \Illuminate\Support\Carbon|null $deleted_at + * @property string $name 姓名 + * @property string $alias 别名 * @property-read \Illuminate\Database\Eloquent\Collection $abouts * @property-read int|null $abouts_count - * @property-read mixed $alias * @property-read \App\Models\Team|null $currentTeam * @property-read mixed $format_gender * @property-read mixed $full_avatar - * @property-read mixed $name * @property-read \Illuminate\Notifications\DatabaseNotificationCollection $notifications * @property-read int|null $notifications_count * @property-read \Illuminate\Database\Eloquent\Collection $ownedTeams @@ -532,6 +532,7 @@ class IdeHelperTerm {} * @method static \Illuminate\Database\Eloquent\Builder|User query() * @method static \Illuminate\Database\Eloquent\Builder|User whereAddress($value) * @method static \Illuminate\Database\Eloquent\Builder|User whereAge($value) + * @method static \Illuminate\Database\Eloquent\Builder|User whereAlias($value) * @method static \Illuminate\Database\Eloquent\Builder|User whereAvatar($value) * @method static \Illuminate\Database\Eloquent\Builder|User whereBirthday($value) * @method static \Illuminate\Database\Eloquent\Builder|User whereBrowser($value) @@ -558,6 +559,7 @@ class IdeHelperTerm {} * @method static \Illuminate\Database\Eloquent\Builder|User whereLastAuthedAt($value) * @method static \Illuminate\Database\Eloquent\Builder|User whereLastName($value) * @method static \Illuminate\Database\Eloquent\Builder|User whereMethod($value) + * @method static \Illuminate\Database\Eloquent\Builder|User whereName($value) * @method static \Illuminate\Database\Eloquent\Builder|User whereNotificationCount($value) * @method static \Illuminate\Database\Eloquent\Builder|User whereOs($value) * @method static \Illuminate\Database\Eloquent\Builder|User whereOsAlias($value) diff --git a/app/Console/Commands/UpdateUserNameAlias.php b/app/Console/Commands/UpdateUserNameAlias.php new file mode 100644 index 0000000..2e5c794 --- /dev/null +++ b/app/Console/Commands/UpdateUserNameAlias.php @@ -0,0 +1,49 @@ +get(); + + $bar = $this->output->createProgressBar(count($users)); + + $bar->start(); + + $users->each(function (User $user) use (&$bar) { + $user->name = $user->first_name . ' ' . $user->last_name; + $user->alias = $user->first_alias . ' ' . $user->last_alias; + $user->save(); + + $this->info('User ' . $user->id . ' name and alias updated'); + + $bar->advance(); + }); + + $bar->finish(); + } +} diff --git a/app/Console/Commands/UpdateUserToAdmin.php b/app/Console/Commands/UpdateUserToAdmin.php index 3915ec0..114c36b 100644 --- a/app/Console/Commands/UpdateUserToAdmin.php +++ b/app/Console/Commands/UpdateUserToAdmin.php @@ -38,7 +38,7 @@ public function handle(): void $this->error("User with ID {$id} not found."); } } else { - $users = User::select(['id', 'first_name', 'last_name', 'email'])->get(); + $users = User::select(['id', 'name', 'email'])->get(); if ($users->isEmpty()) { $this->error('No users found.'); diff --git a/app/Models/User.php b/app/Models/User.php index 1d3bd20..ec72514 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -89,6 +89,8 @@ class User extends Authenticatable 'created_at', 'updated_at', 'deleted_at', + 'name', + 'alias', ]; /** @@ -162,20 +164,6 @@ protected function formatGender(): Attribute ); } - protected function name(): Attribute - { - return Attribute::make( - get: fn (mixed $value, array $attributes) => data_get($attributes, 'first_name') . ' ' . data_get($attributes, 'last_name'), - ); - } - - protected function alias(): Attribute - { - return Attribute::make( - get: fn (mixed $value, array $attributes) => data_get($attributes, 'first_alias') . ' ' . data_get($attributes, 'last_alias'), - ); - } - protected function fullAvatar(): Attribute { return Attribute::make( diff --git a/app/Observers/UserObserver.php b/app/Observers/UserObserver.php index 74ef6f1..234ee9c 100644 --- a/app/Observers/UserObserver.php +++ b/app/Observers/UserObserver.php @@ -33,6 +33,12 @@ public function saving(User $user): void if ($user->isDirty('birthday')) { $user->age = Carbon::parse($user->birthday)->age; } + if ($user->isDirty(['first_name', 'last_name'])) { + $user->name = $user->first_name . ' ' . $user->last_name; + } + if ($user->isDirty(['first_alias', 'last_alias'])) { + $user->alias = $user->first_alias . ' ' . $user->last_alias; + } } /** diff --git a/database/migrations/2024_12_29_030843_add_name_and_alias_to_users_table.php b/database/migrations/2024_12_29_030843_add_name_and_alias_to_users_table.php new file mode 100644 index 0000000..1d29367 --- /dev/null +++ b/database/migrations/2024_12_29_030843_add_name_and_alias_to_users_table.php @@ -0,0 +1,31 @@ +string('name')->comment('姓名'); + $table->string('alias')->comment('别名'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('users', function (Blueprint $table) { + $table->dropColumn(['name', 'alias']); + }); + } +};