Nenhuma descrição

2021_06_05_235926_create_assigned_roles_table.php 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. use Illuminate\Database\Schema\Blueprint;
  3. use Illuminate\Database\Migrations\Migration;
  4. class CreateAssignedRolesTable extends Migration
  5. {
  6. public function up()
  7. {
  8. // Creates the roles table
  9. Schema::create('roles', function ($table) {
  10. $table->increments('id')->unsigned();
  11. $table->string('name')->unique();
  12. $table->timestamps();
  13. });
  14. // Creates the assigned_roles (Many-to-Many relation) table
  15. Schema::create('assigned_roles', function ($table) {
  16. $table->increments('id')->unsigned();
  17. $table->integer('user_id')->unsigned();
  18. $table->integer('role_id')->unsigned();
  19. $table->foreign('user_id')->references('id')->on('users')
  20. ->onUpdate('cascade')->onDelete('cascade');
  21. $table->foreign('role_id')->references('id')->on('roles');
  22. });
  23. // Creates the permissions table
  24. Schema::create('permissions', function ($table) {
  25. $table->increments('id')->unsigned();
  26. $table->string('name')->unique();
  27. $table->string('display_name');
  28. $table->timestamps();
  29. });
  30. // Creates the permission_role (Many-to-Many relation) table
  31. Schema::create('permission_role', function ($table) {
  32. $table->increments('id')->unsigned();
  33. $table->integer('permission_id')->unsigned();
  34. $table->integer('role_id')->unsigned();
  35. $table->foreign('permission_id')->references('id')->on('permissions'); // assumes a users table
  36. $table->foreign('role_id')->references('id')->on('roles');
  37. });
  38. }
  39. /**
  40. * Reverse the migrations.
  41. *
  42. * @return void
  43. */
  44. public function down()
  45. {
  46. Schema::table('assigned_roles', function (Blueprint $table) {
  47. $table->dropForeign('assigned_roles_user_id_foreign');
  48. $table->dropForeign('assigned_roles_role_id_foreign');
  49. });
  50. Schema::table('permission_role', function (Blueprint $table) {
  51. $table->dropForeign('permission_role_permission_id_foreign');
  52. $table->dropForeign('permission_role_role_id_foreign');
  53. });
  54. Schema::drop('assigned_roles');
  55. Schema::drop('permission_role');
  56. Schema::drop('roles');
  57. Schema::drop('permissions');
  58. }
  59. }