From 8b8345ddbc24e9243d5c0fc7b5ab4d5d0bfe6dc2 Mon Sep 17 00:00:00 2001 From: liangyuyan <1103300295@qq.com> Date: Sat, 12 Sep 2020 17:10:21 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=A8=E6=88=B7=E5=9C=B0=E5=9D=80=E8=BF=81?= =?UTF-8?q?=E7=A7=BB,=E5=A4=84=E7=90=86address?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Console/Commands/MigrateUserAddr.php | 37 ++++++++++++++++++------ 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/app/Console/Commands/MigrateUserAddr.php b/app/Console/Commands/MigrateUserAddr.php index 8c5d38a..930e526 100644 --- a/app/Console/Commands/MigrateUserAddr.php +++ b/app/Console/Commands/MigrateUserAddr.php @@ -4,6 +4,7 @@ namespace App\Console\Commands; use Illuminate\Console\Command; use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Schema; class MigrateUserAddr extends Command { @@ -19,7 +20,7 @@ class MigrateUserAddr extends Command * * @var string */ - protected $description = 'migrate user addr data'; + protected $description = 'migrate 迁移用户地址数据'; /** * Create a new command instance. @@ -39,27 +40,45 @@ class MigrateUserAddr extends Command public function handle() { - // get old data from old table - $oldData = DB::table('ims_cjdc_useradd')->get(); + $oldTableName = 'ims_cjdc_useradd'; + $newTableName = 'lanzu_user_address'; + // 判断表是否存在 + if(!Schema::hasTable($oldTableName)){ + var_dump('旧表不存在'); + return 0; + } + if(!Schema::hasTable($newTableName)){ + var_dump('新表不存在'); + return 0; + } + $oldData = DB::table($oldTableName)->get(); $bar = $this->output->createProgressBar(count($oldData)); $bar->start(); $newData = []; foreach ($oldData as $key => $value) { + + $addressArr = explode('|',$value->address); + $address = $addressArr[0]; + $doorplate = isset($addressArr[1])?$addressArr[1]:''; + $newData[] = [ - 'user_id' => $value->user_id ?? '', - 'user_name' => $value->user_name ?? '', - 'address' => $value->address ?? '', + 'id' => $value->id, + 'user_id' => $value->user_id ?? 0, + 'user_name' => $value->user_name == 'undefined'? '': $value->user_name, + 'address' => $address, 'gender' => $value->sex ?? 0, 'lat' => $value->lat ?? '', 'lng' => $value->lng ?? '', - 'tel' => $value->tel ?? '', - 'tags' => json_encode([]), + 'tel' => $value->tel == 'undefined'? '': $value->tel, + 'doorplate' => $doorplate, + 'is_default' => $value->is_default ?? 0, + 'tags' => json_encode(''), 'created_at' => time(), 'updated_at' => time(), ]; - + $bar->advance(); }