Passed
Push — master ( 9c0720...aa2608 )
by Vasyl
01:59
created

CreateTaxonomyTables   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 39
dl 0
loc 98
rs 10
c 0
b 0
f 0
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A createTermablesTable() 0 10 1
A down() 0 6 1
A createVocabulariesTable() 0 7 1
A up() 0 9 1
A createVocabulariablesTable() 0 10 1
A createTermsTable() 0 17 1
1
<?php
2
3
use Illuminate\Support\Facades\Schema;
4
use Illuminate\Database\Schema\Blueprint;
5
use Illuminate\Database\Migrations\Migration;
6
7
class CreateTaxonomyTables extends Migration
8
{
9
    /**
10
     * Run the migrations.
11
     *
12
     * @return void
13
     */
14
    public function up()
15
    {
16
        $this->createVocabulariesTable();
17
        
18
        $this->createTermsTable();
19
        
20
        $this->createVocabulariablesTable();
21
        
22
        $this->createTermablesTable();
23
    }
24
25
    /**
26
     * Taxonomy vocabularies table
27
     */
28
    public function createVocabulariesTable()
29
    {
30
        Schema::create('vocabularies', function (Blueprint $table) {
31
            $table->increments('id');
32
            $table->string('system_name')->unique();
33
            $table->string('name');
34
            $table->text('description')->nullable();
35
            // $table->json('options')->nullable(); // optional, if needed
36
        });
37
    }
38
39
    /**
40
     * Taxonomy terms table
41
     */
42
    public function createTermsTable()
43
    {
44
        Schema::create('terms', function (Blueprint $table) {
45
            $table->increments('id');
46
            $table->string('name');
47
            $table->string('system_name')->nullable()->unique(); // optional
48
            $table->text('description')->nullable();
49
            
50
            // Nested https://github.com/lazychaser/laravel-nestedset - $table->nestedSet();
51
            $table->unsignedInteger('_lft')->default(0);
52
            $table->unsignedInteger('_rgt')->default(0);
53
            $table->unsignedInteger('parent_id')->nullable();
54
55
            $table->integer('weight')->default(0);
56
            $table->string('vocabulary');
57
            // $table->json('options')->nullable(); // optional, if needed
58
            $table->timestamps();
59
        });
60
    }
61
62
    /**
63
     * Таблица сущностей "привязанных" к словарям
64
     */
65
    public function createVocabulariablesTable()
66
    {
67
        Schema::create('vocabularyables', function (Blueprint $table) {
68
            $table->unsignedInteger('vocabulary_id');
69
            $table->morphs('vocabularyable');
70
71
            $table->foreign('vocabulary_id')
72
                ->references('id')
73
                ->on('vocabularies')
74
                ->onDelete('CASCADE');
75
        });
76
    }
77
78
    /**
79
     * Таблица сущностей "привязанных" к термам
80
     */
81
    public function createTermablesTable()
82
    {
83
         Schema::create('termables', function (Blueprint $table) {
84
             $table->unsignedInteger('term_id');
85
             $table->morphs('termable');
86
    
87
             $table->foreign('term_id')
88
                 ->references('id')
89
                 ->on('terms')
90
                 ->onDelete('CASCADE');
91
         });
92
    }
93
94
    /**
95
     * Reverse the migrations.
96
     *
97
     * @return void
98
     */
99
    public function down()
100
    {
101
        Schema::dropIfExists('termables');
102
        Schema::dropIfExists('vocabularyables');
103
        Schema::dropIfExists('terms');
104
        Schema::dropIfExists('vocabularies');
105
    }
106
}
107