CreateUserPermissionTable::up()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
use Helldar\Roles\Support\Database\BaseMigration;
4
use Illuminate\Database\Schema\Blueprint;
5
6
class CreateUserPermissionTable extends BaseMigration
7
{
8
    public function up()
9
    {
10
        $this->createPivot($this->user_permission, $this->users, $this->permissions, 'user_id', 'permission_id');
11
    }
12
13
    public function down()
14
    {
15
        $this->schema()->disableForeignKeyConstraints();
16
17
        $this->dropTables($this->user_permission);
18
19
        $this->schema()->enableForeignKeyConstraints();
20
    }
21
22
    protected function createPivot(string $table, string $first_table, string $second_table, string $first_key, string $second_key)
23
    {
24
        $this->create($table, function (Blueprint $table) use ($first_table, $second_table, $first_key, $second_key) {
25
            $table->unsignedBigInteger($first_key)->index();
26
            $table->unsignedBigInteger($second_key);
27
28
            $table->foreign($first_key)->references('id')->on($first_table)->onDelete('cascade');
29
            $table->foreign($second_key)->references('id')->on($second_table)->onDelete('cascade');
30
        });
31
    }
32
}
33