2020_05_09_110359_laratrust_setup_tables.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. use Illuminate\Database\Migrations\Migration;
  3. use Illuminate\Database\Schema\Blueprint;
  4. use Illuminate\Support\Facades\Schema;
  5. class LaratrustSetupTables extends Migration
  6. {
  7. /**
  8. * Run the migrations.
  9. *
  10. * @return void
  11. */
  12. public function up()
  13. {
  14. // Create table for storing roles
  15. Schema::create('roles', function (Blueprint $table) {
  16. $table->increments('id');
  17. $table->string('name')->unique();
  18. $table->string('display_name')->nullable();
  19. $table->string('description')->nullable();
  20. $table->timestamps();
  21. });
  22. // Create table for storing permissions
  23. Schema::create('permissions', function (Blueprint $table) {
  24. $table->increments('id');
  25. $table->string('name')->unique();
  26. $table->string('display_name')->nullable();
  27. $table->string('description')->nullable();
  28. $table->timestamps();
  29. });
  30. // Create table for associating roles to users and teams (Many To Many Polymorphic)
  31. Schema::create('role_user', function (Blueprint $table) {
  32. $table->unsignedInteger('role_id');
  33. $table->unsignedInteger('user_id');
  34. $table->string('user_type');
  35. $table->foreign('role_id')->references('id')->on('roles')
  36. ->onUpdate('cascade')->onDelete('cascade');
  37. $table->primary(['user_id', 'role_id', 'user_type']);
  38. });
  39. // Create table for associating permissions to users (Many To Many Polymorphic)
  40. Schema::create('permission_user', function (Blueprint $table) {
  41. $table->unsignedInteger('permission_id');
  42. $table->unsignedInteger('user_id');
  43. $table->string('user_type');
  44. $table->foreign('permission_id')->references('id')->on('permissions')
  45. ->onUpdate('cascade')->onDelete('cascade');
  46. $table->primary(['user_id', 'permission_id', 'user_type']);
  47. });
  48. // Create table for associating permissions to roles (Many-to-Many)
  49. Schema::create('permission_role', function (Blueprint $table) {
  50. $table->unsignedInteger('permission_id');
  51. $table->unsignedInteger('role_id');
  52. $table->foreign('permission_id')->references('id')->on('permissions')
  53. ->onUpdate('cascade')->onDelete('cascade');
  54. $table->foreign('role_id')->references('id')->on('roles')
  55. ->onUpdate('cascade')->onDelete('cascade');
  56. $table->primary(['permission_id', 'role_id']);
  57. });
  58. }
  59. /**
  60. * Reverse the migrations.
  61. *
  62. * @return void
  63. */
  64. public function down()
  65. {
  66. Schema::dropIfExists('permission_user');
  67. Schema::dropIfExists('permission_role');
  68. Schema::dropIfExists('role_user');
  69. Schema::dropIfExists('permissions');
  70. Schema::dropIfExists('roles');
  71. Schema::dropIfExists('users');
  72. }
  73. }