No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2020_03_11_041714_create_permission_tables.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. use Illuminate\Support\Facades\Schema;
  3. use Illuminate\Database\Schema\Blueprint;
  4. use Illuminate\Database\Migrations\Migration;
  5. class CreatePermissionTables extends Migration
  6. {
  7. /**
  8. * Run the migrations.
  9. *
  10. * @return void
  11. */
  12. public function up()
  13. {
  14. $tableNames = config('permission.table_names');
  15. $columnNames = config('permission.column_names');
  16. if (empty($tableNames)) {
  17. throw new \Exception('Error: config/permission.php not found and defaults could not be merged. Please publish the package configuration before proceeding.');
  18. }
  19. Schema::create($tableNames['permissions'], function (Blueprint $table) {
  20. $table->bigIncrements('id');
  21. $table->string('name');
  22. $table->string('guard_name');
  23. $table->timestamps();
  24. });
  25. Schema::create($tableNames['roles'], function (Blueprint $table) {
  26. $table->bigIncrements('id');
  27. $table->string('name');
  28. $table->string('guard_name');
  29. $table->timestamps();
  30. });
  31. Schema::create($tableNames['model_has_permissions'], function (Blueprint $table) use ($tableNames, $columnNames) {
  32. $table->unsignedBigInteger('permission_id');
  33. $table->string('model_type');
  34. $table->unsignedBigInteger($columnNames['model_morph_key']);
  35. $table->index([$columnNames['model_morph_key'], 'model_type'], 'model_has_permissions_model_id_model_type_index');
  36. $table->foreign('permission_id')
  37. ->references('id')
  38. ->on($tableNames['permissions'])
  39. ->onDelete('cascade');
  40. $table->primary(['permission_id', $columnNames['model_morph_key'], 'model_type'],
  41. 'model_has_permissions_permission_model_type_primary');
  42. });
  43. Schema::create($tableNames['model_has_roles'], function (Blueprint $table) use ($tableNames, $columnNames) {
  44. $table->unsignedBigInteger('role_id');
  45. $table->string('model_type');
  46. $table->unsignedBigInteger($columnNames['model_morph_key']);
  47. $table->index([$columnNames['model_morph_key'], 'model_type'], 'model_has_roles_model_id_model_type_index');
  48. $table->foreign('role_id')
  49. ->references('id')
  50. ->on($tableNames['roles'])
  51. ->onDelete('cascade');
  52. $table->primary(['role_id', $columnNames['model_morph_key'], 'model_type'],
  53. 'model_has_roles_role_model_type_primary');
  54. });
  55. Schema::create($tableNames['role_has_permissions'], function (Blueprint $table) use ($tableNames) {
  56. $table->unsignedBigInteger('permission_id');
  57. $table->unsignedBigInteger('role_id');
  58. $table->foreign('permission_id')
  59. ->references('id')
  60. ->on($tableNames['permissions'])
  61. ->onDelete('cascade');
  62. $table->foreign('role_id')
  63. ->references('id')
  64. ->on($tableNames['roles'])
  65. ->onDelete('cascade');
  66. $table->primary(['permission_id', 'role_id'], 'role_has_permissions_permission_id_role_id_primary');
  67. });
  68. app('cache')
  69. ->store(config('permission.cache.store') != 'default' ? config('permission.cache.store') : null)
  70. ->forget(config('permission.cache.key'));
  71. }
  72. /**
  73. * Reverse the migrations.
  74. *
  75. * @return void
  76. */
  77. public function down()
  78. {
  79. $tableNames = config('permission.table_names');
  80. if (empty($tableNames)) {
  81. throw new \Exception('Error: config/permission.php not found and defaults could not be merged. Please publish the package configuration before proceeding, or drop the tables manually.');
  82. }
  83. Schema::drop($tableNames['role_has_permissions']);
  84. Schema::drop($tableNames['model_has_roles']);
  85. Schema::drop($tableNames['model_has_permissions']);
  86. Schema::drop($tableNames['roles']);
  87. Schema::drop($tableNames['permissions']);
  88. }
  89. }