2016_01_04_173148_create_admin_tables.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. use Illuminate\Database\Migrations\Migration;
  3. use Illuminate\Database\Schema\Blueprint;
  4. use Illuminate\Support\Facades\Schema;
  5. class CreateAdminTables extends Migration
  6. {
  7. public function getConnection()
  8. {
  9. return $this->config('database.connection') ?: config('database.default');
  10. }
  11. public function config($key)
  12. {
  13. return config('admin.' . $key);
  14. }
  15. /**
  16. * Run the migrations.
  17. *
  18. * @return void
  19. */
  20. public function up()
  21. {
  22. Schema::create($this->config('database.users_table'), function (Blueprint $table) {
  23. $table->bigIncrements('id');
  24. $table->string('username', 120)->unique();
  25. $table->string('password', 80);
  26. $table->string('name');
  27. $table->string('avatar')->nullable();
  28. $table->string('remember_token', 100)->nullable();
  29. $table->timestamps();
  30. });
  31. Schema::create($this->config('database.roles_table'), function (Blueprint $table) {
  32. $table->bigIncrements('id');
  33. $table->string('name', 50);
  34. $table->string('slug', 50)->unique();
  35. $table->timestamps();
  36. });
  37. Schema::create($this->config('database.permissions_table'), function (Blueprint $table) {
  38. $table->bigIncrements('id');
  39. $table->string('name', 50);
  40. $table->string('slug', 50)->unique();
  41. $table->string('http_method')->nullable();
  42. $table->text('http_path')->nullable();
  43. $table->integer('order')->default(0);
  44. $table->bigInteger('parent_id')->default(0);
  45. $table->timestamps();
  46. });
  47. Schema::create($this->config('database.menu_table'), function (Blueprint $table) {
  48. $table->bigIncrements('id');
  49. $table->bigInteger('parent_id')->default(0);
  50. $table->integer('order')->default(0);
  51. $table->string('title', 50);
  52. $table->string('icon', 50)->nullable();
  53. $table->string('uri', 50)->nullable();
  54. $table->timestamps();
  55. });
  56. Schema::create($this->config('database.role_users_table'), function (Blueprint $table) {
  57. $table->bigInteger('role_id');
  58. $table->bigInteger('user_id');
  59. $table->unique(['role_id', 'user_id']);
  60. $table->timestamps();
  61. });
  62. Schema::create($this->config('database.role_permissions_table'), function (Blueprint $table) {
  63. $table->bigInteger('role_id');
  64. $table->bigInteger('permission_id');
  65. $table->unique(['role_id', 'permission_id']);
  66. $table->timestamps();
  67. });
  68. Schema::create($this->config('database.role_menu_table'), function (Blueprint $table) {
  69. $table->bigInteger('role_id');
  70. $table->bigInteger('menu_id');
  71. $table->unique(['role_id', 'menu_id']);
  72. $table->timestamps();
  73. });
  74. Schema::create($this->config('database.permission_menu_table'), function (Blueprint $table) {
  75. $table->bigInteger('permission_id');
  76. $table->bigInteger('menu_id');
  77. $table->unique(['permission_id', 'menu_id']);
  78. $table->timestamps();
  79. });
  80. }
  81. /**
  82. * Reverse the migrations.
  83. *
  84. * @return void
  85. */
  86. public function down()
  87. {
  88. Schema::dropIfExists($this->config('database.users_table'));
  89. Schema::dropIfExists($this->config('database.roles_table'));
  90. Schema::dropIfExists($this->config('database.permissions_table'));
  91. Schema::dropIfExists($this->config('database.menu_table'));
  92. Schema::dropIfExists($this->config('database.role_users_table'));
  93. Schema::dropIfExists($this->config('database.role_permissions_table'));
  94. Schema::dropIfExists($this->config('database.role_menu_table'));
  95. Schema::dropIfExists($this->config('database.permission_menu_table'));
  96. }
  97. }