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

CreateUserPermissionTable::createPivot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 5
dl 0
loc 8
rs 10
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