|
2
|
1 <?php
|
|
|
2
|
|
|
3 namespace Libraries;
|
|
|
4
|
|
|
5 use Illuminate\Database\Schema\ColumnDefinition;
|
|
|
6
|
|
|
7 class MyBlueprint extends \Illuminate\Database\Schema\Blueprint
|
|
|
8 {
|
|
|
9 /**
|
|
|
10 * Generates the base columns for a regular table
|
|
|
11 */
|
|
|
12 public function baseColumns(): void
|
|
|
13 {
|
|
|
14 $this->id();
|
|
|
15 $this->reference('users', 'created_by')->nullable();
|
|
|
16 $this->reference('users', 'updated_by')->nullable();
|
|
|
17 $this->timestamps();
|
|
|
18 $this->softDeletes();
|
|
|
19 }
|
|
|
20
|
|
|
21 /**
|
|
|
22 * @param mixed $abbreviation
|
|
|
23 */
|
|
|
24 public function baseTextColumns($abbreviation = false): void
|
|
|
25 {
|
|
|
26 $this->string('name', 255);
|
|
|
27 $this->text('description')->nullable();
|
|
|
28 if ($abbreviation) {
|
|
|
29 $this->string('abbreviation', 50)->nullable();
|
|
|
30 }
|
|
|
31 }
|
|
|
32
|
|
|
33 public function pivotColumns(string $table1, string $table2): void
|
|
|
34 {
|
|
|
35 $this->comment('PIVOT:'.json_encode(['table1' => $table1, 'table2' => $table2]));
|
|
|
36 $this->id();
|
|
|
37 $this->reference($table1);
|
|
|
38 $this->reference($table2);
|
|
|
39 }
|
|
|
40
|
|
|
41 public function settingsColumns(): void
|
|
|
42 {
|
|
|
43 $this->baseColumns();
|
|
|
44 $this->baseTextColumns();
|
|
|
45 }
|
|
|
46
|
|
|
47 /**
|
|
|
48 * @param mixed $keep_column
|
|
|
49 */
|
|
|
50 public function dropReference(string $table, ?string $override_column_name = null, $keep_column = false): void
|
|
|
51 {
|
|
|
52 // set up relevant strings
|
|
|
53 $column_name = self::get_foreign_column_name($table, $override_column_name);
|
|
|
54 $fk_name = self::get_foreign_key_name($this->getTable(), $table, $column_name);
|
|
|
55
|
|
|
56 // drop the reference
|
|
|
57 $this->dropForeign($fk_name);
|
|
|
58
|
|
|
59 // drop the column if necessary
|
|
|
60 if (! $keep_column) {
|
|
|
61 $this->dropColumn($column_name);
|
|
|
62 }
|
|
|
63 }
|
|
|
64
|
|
|
65 public function reference(string $table, ?string $override_column_name = null): ColumnDefinition
|
|
|
66 {
|
|
|
67 // set up relevant strings
|
|
|
68 $column_name = self::get_foreign_column_name($table, $override_column_name);
|
|
|
69 $fk_name = self::get_foreign_key_name($this->getTable(), $table, $column_name);
|
|
|
70
|
|
|
71 // create the column
|
|
|
72 $column = $this->unsignedBigInteger($column_name);
|
|
|
73
|
|
|
74 // create the foreign key
|
|
|
75 $this->foreign($column_name, $fk_name)->references('id')->on($table);
|
|
|
76
|
|
|
77 return $column;
|
|
|
78 }
|
|
|
79
|
|
|
80 protected static function get_foreign_column_name(string $input, ?string $override = null): string
|
|
|
81 {
|
|
|
82 return ! is_null($override) ? $override : \Str::singular($input).'_id';
|
|
|
83 }
|
|
|
84
|
|
|
85 /*
|
|
|
86 * Generates a foreign key name
|
|
|
87 * 'FK_' + current table name + foreign column name
|
|
|
88 *
|
|
|
89 * */
|
|
|
90 protected static function get_foreign_key_name(string $current_table, string $table, string $column_name): string
|
|
|
91 {
|
|
|
92 $fk_name = 'FK_';
|
|
|
93
|
|
|
94 // add the current table name
|
|
|
95 $fk_name .= \Str::singular($current_table);
|
|
|
96
|
|
|
97 // add the foreign column name
|
|
|
98 $fk_name .= '_'.$column_name;
|
|
|
99
|
|
|
100 return $fk_name;
|
|
|
101 }
|
|
|
102 }
|