Blame view

database/migrations/2017_09_01_000000_create_roles_table.php 2.94 KB
b7c7a5f6   Alexey Boroda   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
  <?php
  use Illuminate\Database\Migrations\Migration;
  use Illuminate\Database\Schema\Blueprint;
  
  class CreateRolesTable extends Migration
  {
      /**
       * Run the migrations.
       *
       * @return  void
       */
      public function up()
      {
          // Create table for storing roles
          Schema::create('roles', function (Blueprint $table) {
              $table->increments('id');
              $table->string('name');
              $table->string('display_name');
              $table->string('description')->nullable();
              $table->timestamps();
  
              $table->unique('name');
          });
  
          // Create table for associating roles to users (Many To Many Polymorphic)
          Schema::create('user_roles', function (Blueprint $table) {
              $table->integer('user_id')->unsigned();
              $table->integer('role_id')->unsigned();
              $table->string('user_type');
  
              $table->foreign('role_id')->references('id')->on('roles')
                  ->onUpdate('cascade')->onDelete('cascade');
  
              $table->primary(['user_id', 'role_id', 'user_type']);
          });
  
          // Create table for storing permissions
          Schema::create('permissions', function (Blueprint $table) {
              $table->increments('id');
              $table->string('name');
              $table->string('display_name');
              $table->string('description')->nullable();
              $table->timestamps();
  
              $table->unique('name');
          });
  
          // Create table for associating permissions to roles (Many-to-Many)
          Schema::create('role_permissions', function (Blueprint $table) {
              $table->integer('role_id')->unsigned();
              $table->integer('permission_id')->unsigned();
  
              $table->foreign('role_id')->references('id')->on('roles')
                  ->onUpdate('cascade')->onDelete('cascade');
              $table->foreign('permission_id')->references('id')->on('permissions')
                  ->onUpdate('cascade')->onDelete('cascade');
  
              $table->primary(['role_id', 'permission_id']);
          });
  
          // Create table for associating permissions to users (Many To Many Polymorphic)
          Schema::create('user_permissions', function (Blueprint $table) {
              $table->integer('user_id')->unsigned();
              $table->integer('permission_id')->unsigned();
              $table->string('user_type');
  
              $table->foreign('permission_id')->references('id')->on('permissions')
                  ->onUpdate('cascade')->onDelete('cascade');
  
              $table->primary(['user_id', 'permission_id', 'user_type']);
          });
  
      }
  
      /**
       * Reverse the migrations.
       *
       * @return  void
       */
      public function down()
      {
          // Cascade table first
          Schema::dropIfExists('user_permissions');
          Schema::dropIfExists('role_permissions');
          Schema::dropIfExists('permissions');
          Schema::dropIfExists('user_roles');
          Schema::dropIfExists('roles');
      }
  }