CreatePermissionTables   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 2
dl 0
loc 95
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A down() 0 10 1
A up() 0 71 1
1
<?php
2
3
use Illuminate\Database\Migrations\Migration;
4
use Illuminate\Database\Schema\Blueprint;
5
6
class CreatePermissionTables extends Migration
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
7
{
8
    /**
9
     * Run the migrations.
10
     *
11
     * @return void
12
     */
13
    public function up()
14
    {
15
        $config = config('laravel-permission.table_names');
16
17
        Schema::create($config['roles'], function (Blueprint $table) {
18
            $table->increments('id');
19
            $table->string('name')->unique();
20
            $table->string('display_name')->nullable();
21
            $table->string('description')->nullable();
22
            $table->timestamps();
23
        });
24
25
        Schema::create($config['permissions'], function (Blueprint $table) {
26
            $table->increments('id');
27
            $table->string('name')->unique();
28
            $table->string('display_name')->nullable();
29
            $table->string('description')->nullable();
30
            $table->timestamps();
31
        });
32
33
        Schema::create($config['user_has_permissions'], function (Blueprint $table) use ($config) {
34
            $table->integer('user_id')->unsigned();
35
            $table->integer('permission_id')->unsigned();
36
37
            $table->foreign('user_id')
38
                ->references('id')
39
                ->on($config['users'])
40
                ->onDelete('cascade');
41
42
            $table->foreign('permission_id')
43
                ->references('id')
44
                ->on($config['permissions'])
45
                ->onDelete('cascade');
46
47
            $table->primary(['user_id', 'permission_id']);
48
        });
49
50
        Schema::create($config['user_has_roles'], function (Blueprint $table) use ($config) {
51
            $table->integer('role_id')->unsigned();
52
            $table->integer('user_id')->unsigned();
53
54
            $table->foreign('role_id')
55
                ->references('id')
56
                ->on($config['roles'])
57
                ->onDelete('cascade');
58
59
            $table->foreign('user_id')
60
                ->references('id')
61
                ->on($config['users'])
62
                ->onDelete('cascade');
63
64
            $table->primary(['role_id', 'user_id']);
65
        });
66
67
        Schema::create($config['role_has_permissions'], function (Blueprint $table) use ($config) {
68
            $table->integer('permission_id')->unsigned();
69
            $table->integer('role_id')->unsigned();
70
71
            $table->foreign('permission_id')
72
                ->references('id')
73
                ->on($config['permissions'])
74
                ->onDelete('cascade');
75
76
            $table->foreign('role_id')
77
                ->references('id')
78
                ->on($config['roles'])
79
                ->onDelete('cascade');
80
81
            $table->primary(['permission_id', 'role_id']);
82
        });
83
    }
84
85
    /**
86
     * Reverse the migrations.
87
     *
88
     * @return void
89
     */
90
    public function down()
91
    {
92
        $config = config('laravel-permission.table_names');
93
94
        Schema::drop($config['role_has_permissions']);
95
        Schema::drop($config['user_has_roles']);
96
        Schema::drop($config['user_has_permissions']);
97
        Schema::drop($config['roles']);
98
        Schema::drop($config['permissions']);
99
    }
100
}
101