Passed
Push — master ( 4688c8...2d21c0 )
by Andrey
12:30
created

CreateUserPermissionTable   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 24
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A down() 0 7 1
A up() 0 3 1
A createPivot() 0 8 1
1
<?php
2
3
use Helldar\Roles\Support\Database\BaseMigration;
4
use Illuminate\Database\Schema\Blueprint;
5
6
;
7
8
class CreateUserPermissionTable extends BaseMigration
9
{
10
    public function up()
11
    {
12
        $this->createPivot($this->user_permission, $this->users, $this->permissions, 'user_id', 'permission_id');
13
    }
14
15
    public function down()
16
    {
17
        $this->schema()->disableForeignKeyConstraints();
18
19
        $this->dropTables($this->user_permission);
20
21
        $this->schema()->enableForeignKeyConstraints();
22
    }
23
24
    protected function createPivot(string $table, string $first_table, string $second_table, string $first_key, string $second_key)
25
    {
26
        $this->create($table, function (Blueprint $table) use ($first_table, $second_table, $first_key, $second_key) {
27
            $table->unsignedBigInteger($first_key)->index();
28
            $table->unsignedBigInteger($second_key);
29
30
            $table->foreign($first_key)->references('id')->on($first_table)->onDelete('cascade');
31
            $table->foreign($second_key)->references('id')->on($second_table)->onDelete('cascade');
32
        });
33
    }
34
}
35