2019_11_22_094213_create_units_table.php 1013 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. use Illuminate\Database\Migrations\Migration;
  3. use Illuminate\Database\Schema\Blueprint;
  4. use Illuminate\Support\Facades\Schema;
  5. use Illuminate\Support\Facades\DB;
  6. class CreateUnitsTable extends Migration
  7. {
  8. protected $units = ['kg','m³','T','件', '托',"箱",'单',"日","月","m²","年"];
  9. /**
  10. * Run the migrations.
  11. *
  12. * @return void
  13. */
  14. public function up()
  15. {
  16. Schema::create('units', function (Blueprint $table) {
  17. $table->bigIncrements('id');
  18. $table->timestamps();
  19. $table->string('name',50)->unique()->comment('名称');
  20. });
  21. for ($i=0;$i<count($this->units);$i++){
  22. DB::insert(DB::raw("INSERT INTO units(name,created_at,updated_at) VALUES(?,?,?)"),[
  23. $this->units[$i],date("Y-m-d"),date("Y-m-d")
  24. ]);
  25. }
  26. }
  27. /**
  28. * Reverse the migrations.
  29. *
  30. * @return void
  31. */
  32. public function down()
  33. {
  34. Schema::dropIfExists('units');
  35. }
  36. }