1
|
|
|
<?php namespace jlourenco\support\Database; |
2
|
|
|
|
3
|
|
|
use Illuminate\Database\Schema\Blueprint as IlluminateBlueprint; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Extended version of Blueprint with |
7
|
|
|
* support of 'set' data type |
8
|
|
|
*/ |
9
|
|
|
class Blueprint extends IlluminateBlueprint { |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Create a new binary column on the table. |
13
|
|
|
* |
14
|
|
|
* @param string $column |
15
|
|
|
* @param int $length |
16
|
|
|
* @return \Illuminate\Support\Fluent |
17
|
|
|
*/ |
18
|
|
|
public function binary($column, $length = 255) |
19
|
|
|
{ |
20
|
|
|
return $this->addColumn('binary', $column, compact('length')); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Add creation and update user information to the table. |
25
|
|
|
* |
26
|
|
|
* @return void |
27
|
|
|
*/ |
28
|
|
|
public function creation() |
29
|
|
|
{ |
30
|
|
|
$this->integer('created_by')->nullable()->unsigned(); |
31
|
|
|
$this->integer('modified_by')->nullable()->unsigned(); |
32
|
|
|
$this->integer('deleted_by')->nullable()->unsigned(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function creationRelation() |
36
|
|
|
{ |
37
|
|
|
$this->foreign('created_by')->references('id')->on('User'); |
38
|
|
|
$this->foreign('modified_by')->references('id')->on('User'); |
39
|
|
|
$this->foreign('deleted_by')->references('id')->on('User'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Create a new 'set' column on the table. |
44
|
|
|
* |
45
|
|
|
* @param string $column |
46
|
|
|
* @param array $allowed |
47
|
|
|
* @return \Illuminate\Support\Fluent |
48
|
|
|
*/ |
49
|
|
|
public function set($column, array $allowed) |
50
|
|
|
{ |
51
|
|
|
return $this->addColumn('set', $column, compact('allowed')); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Specify a unique index for the table. |
56
|
|
|
* |
57
|
|
|
* @param string|array $columns |
58
|
|
|
* @param string $name |
59
|
|
|
* @param int $length |
60
|
|
|
* @return \Illuminate\Support\Fluent |
61
|
|
|
*/ |
62
|
|
|
public function unique($columns, $name = null, $length = null) |
63
|
|
|
{ |
64
|
|
|
return $this->indexCommand('unique', $columns, $name, $length); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Specify an index for the table. |
69
|
|
|
* |
70
|
|
|
* @param string|array $columns |
71
|
|
|
* @param string $name |
72
|
|
|
* @param int $length |
73
|
|
|
* @return \Illuminate\Support\Fluent |
74
|
|
|
*/ |
75
|
|
|
public function index($columns, $name = null, $length = null) |
76
|
|
|
{ |
77
|
|
|
return $this->indexCommand('index', $columns, $name, $length); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Determine if the given table exists. |
82
|
|
|
* |
83
|
|
|
* @param string $table |
84
|
|
|
* |
85
|
|
|
* @return bool |
86
|
|
|
*/ |
87
|
|
|
public function hasForeign($table, $foreign) |
88
|
|
|
{ |
89
|
|
|
$sql = $this->grammar->compileHasForeign(); |
|
|
|
|
90
|
|
|
$table = $this->connection->getTablePrefix() . $table; |
|
|
|
|
91
|
|
|
return count($this->connection->select($sql, [$table, $foreign])) > 0; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Add a new index command to the blueprint. |
96
|
|
|
* |
97
|
|
|
* @param string $type |
98
|
|
|
* @param string|array $columns |
99
|
|
|
* @param string $index |
100
|
|
|
* @param int $length |
101
|
|
|
* @return \Illuminate\Support\Fluent |
102
|
|
|
*/ |
103
|
|
|
protected function indexCommand($type, $columns, $index, $length = null) |
104
|
|
|
{ |
105
|
|
|
$columns = (array) $columns; |
106
|
|
|
// If no name was specified for this index, we will create one using a basic |
107
|
|
|
// convention of the table name, followed by the columns, followed by an |
108
|
|
|
// index type, such as primary or index, which makes the index unique. |
109
|
|
|
if (is_null($index)) |
110
|
|
|
$index = $this->createIndexName($type, $columns); |
111
|
|
|
return $this->addCommand($type, compact('index', 'columns', 'length')); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
} |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: