diff --git a/app/Models/User.php b/app/Models/User.php index 535b158..7508cdd 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -158,7 +158,7 @@ public function user_group(): BelongsTo public function scopeBirthday() { - return $this->select(['id', 'name', 'birthday_at', 'email', 'created_at'])->whereMonth('birthday_at', now()->month) + return $this->select(['id', 'name', 'birthday_at', 'email_md5', 'created_at'])->whereMonth('birthday_at', now()->month) ->whereDay('birthday_at', now()->day); } } diff --git a/app/Models/WorkOrder/Reply.php b/app/Models/WorkOrder/Reply.php index 59e2f91..6612d18 100644 --- a/app/Models/WorkOrder/Reply.php +++ b/app/Models/WorkOrder/Reply.php @@ -149,7 +149,7 @@ public function scopeWorkOrderId($query, $work_order_id) public function scopeWithUser($query) { return $query->with(['user' => function ($query) { - $query->select('id', 'name', 'email'); + $query->select('id', 'name', 'email_md5'); }]); } } diff --git a/database/migrations/2023_01_02_220622_add_email_md5_to_users_table.php b/database/migrations/2023_01_02_220622_add_email_md5_to_users_table.php new file mode 100644 index 0000000..2c3fd76 --- /dev/null +++ b/database/migrations/2023_01_02_220622_add_email_md5_to_users_table.php @@ -0,0 +1,45 @@ +string('email_md5')->after('email')->nullable()->comment('邮箱 MD5'); + }); + + $count = User::count(); + $i = 0; + + User::chunk(100, function ($users) use (&$i, $count) { + foreach ($users as $user) { + + echo sprintf('Updating %d/%d', ++$i, $count) . PHP_EOL; + + $user->email_md5 = md5($user->email); + $user->saveQuietly(); + } + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down(): void + { + Schema::table('users', function (Blueprint $table) { + $table->dropColumn('email_md5'); + }); + } +};