Skip to content

Commit

Permalink
feat(users): 数据模型增加 name 和 alias 字段 (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaiyuxin103 committed Dec 29, 2024
1 parent c12c972 commit 1294b48
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 17 deletions.
6 changes: 4 additions & 2 deletions _ide_helper_models.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, \App\Models\About> $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<int, \Illuminate\Notifications\DatabaseNotification> $notifications
* @property-read int|null $notifications_count
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Team> $ownedTeams
Expand All @@ -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)
Expand All @@ -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)
Expand Down
49 changes: 49 additions & 0 deletions app/Console/Commands/UpdateUserNameAlias.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace App\Console\Commands;

use App\Models\User;
use Illuminate\Console\Command;

class UpdateUserNameAlias extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:update-user-name-alias';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';

/**
* Execute the console command.
*/
public function handle(): void
{
$users = User::withTrashed()->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();
}
}
2 changes: 1 addition & 1 deletion app/Console/Commands/UpdateUserToAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
Expand Down
16 changes: 2 additions & 14 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ class User extends Authenticatable
'created_at',
'updated_at',
'deleted_at',
'name',
'alias',
];

/**
Expand Down Expand Up @@ -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(
Expand Down
6 changes: 6 additions & 0 deletions app/Observers/UserObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
$table->string('name')->comment('姓名');
$table->string('alias')->comment('别名');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn(['name', 'alias']);
});
}
};

0 comments on commit 1294b48

Please sign in to comment.