From d5efe9b48553f0a969344096a31d56bccd015630 Mon Sep 17 00:00:00 2001 From: "iVampireSP.com" Date: Mon, 2 Jan 2023 22:26:44 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=20=E5=B0=86=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E5=A4=B4=E5=83=8F=E7=9A=84=E5=9C=B0=E6=96=B9=E7=9A=84?= =?UTF-8?q?=20email=20=E6=94=B9=E6=88=90=20email=5Fmd5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Models/User.php | 2 +- app/Models/WorkOrder/Reply.php | 2 +- ...02_220622_add_email_md5_to_users_table.php | 45 +++++++++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 database/migrations/2023_01_02_220622_add_email_md5_to_users_table.php 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'); + }); + } +};