2020_08_20_140853_alter_pre_users.php 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. use Illuminate\Database\Migrations\Migration;
  3. use Illuminate\Database\Schema\Blueprint;
  4. use Illuminate\Support\Facades\Schema;
  5. class AlterPreUsers extends Migration
  6. {
  7. /**
  8. * Run the migrations.
  9. *
  10. * @return void
  11. */
  12. public function up()
  13. {
  14. Schema::table('users', function (Blueprint $table) {
  15. $table->string('az', 10)->after('token')->nullable()->default('')->index('IDEX_az')->comment('A-Z');
  16. });
  17. //
  18. $lists = \App\Module\Base::DBC2A(DB::table('users')->select(['id','username','nickname'])->where('az', '')->get());
  19. foreach ($lists AS $item) {
  20. DB::table('users')->where('id', $item['id'])->update([
  21. 'az' => \App\Module\Base::getFirstCharter($item['nickname'] ?: $item['username'])
  22. ]);
  23. }
  24. }
  25. /**
  26. * Reverse the migrations.
  27. *
  28. * @return void
  29. */
  30. public function down()
  31. {
  32. Schema::table('users', function (Blueprint $table) {
  33. $table->dropColumn('az');
  34. });
  35. }
  36. }