2018_04_17_070220_entrust_setup_tables.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. use Illuminate\Database\Migrations\Migration;
  3. use Illuminate\Database\Schema\Blueprint;
  4. class EntrustSetupTables extends Migration
  5. {
  6. /**
  7. * Run the migrations.
  8. *
  9. * @return void
  10. */
  11. public function up()
  12. {
  13. // Create table for storing roles
  14. if (!Schema::hasTable('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. }
  23. // Create table for associating roles to users (Many-to-Many)
  24. if (!Schema::hasTable('role_user')){
  25. Schema::create('role_user', function (Blueprint $table) {
  26. $table->integer('user_id')->unsigned();
  27. $table->integer('role_id')->unsigned();
  28. $table->foreign('user_id')->references('id')->on('users')
  29. ->onUpdate('cascade')->onDelete('cascade');
  30. $table->foreign('role_id')->references('id')->on('roles')
  31. ->onUpdate('cascade')->onDelete('cascade');
  32. $table->primary(['user_id', 'role_id']);
  33. });
  34. }
  35. // Create table for storing permissions
  36. if (!Schema::hasTable('permissions')){
  37. Schema::create('permissions', function (Blueprint $table) {
  38. $table->increments('id');
  39. $table->string('name')->unique();
  40. $table->string('display_name')->nullable();
  41. $table->string('description')->nullable();
  42. $table->timestamps();
  43. });
  44. }
  45. // Create table for associating permissions to roles (Many-to-Many)
  46. if (!Schema::hasTable('permission_role')){
  47. Schema::create('permission_role', function (Blueprint $table) {
  48. $table->integer('permission_id')->unsigned();
  49. $table->integer('role_id')->unsigned();
  50. $table->foreign('permission_id')->references('id')->on('permissions')
  51. ->onUpdate('cascade')->onDelete('cascade');
  52. $table->foreign('role_id')->references('id')->on('roles')
  53. ->onUpdate('cascade')->onDelete('cascade');
  54. $table->primary(['permission_id', 'role_id']);
  55. });
  56. }
  57. }
  58. /**
  59. * Reverse the migrations.
  60. *
  61. * @return void
  62. */
  63. public function down()
  64. {
  65. Schema::drop('permission_role');
  66. Schema::drop('permissions');
  67. Schema::drop('role_user');
  68. Schema::drop('roles');
  69. }
  70. }