|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Reedware\LaravelBlueprints\Concerns; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
|
6
|
|
|
|
|
7
|
|
|
trait BelongsTo |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Defines a Belongs To Column and Relationship. |
|
11
|
|
|
* |
|
12
|
|
|
* @param string $table |
|
13
|
|
|
* @param string|null $foreign |
|
14
|
|
|
* @param string|null $local |
|
15
|
|
|
* @param string|null $name |
|
16
|
|
|
* @param string|null $onUpdate |
|
17
|
|
|
* @param string|null $onDelete |
|
18
|
|
|
* |
|
19
|
|
|
* @return \Illuminate\Support\Fluent |
|
20
|
|
|
*/ |
|
21
|
|
|
public function belongsTo($table, $foreign = null, $local = null, $name = null, $onUpdate = null, $onDelete = null) |
|
22
|
|
|
{ |
|
23
|
|
|
// Determine the Name of the Foreign Key |
|
24
|
|
|
if(is_null($foreign)) { |
|
25
|
|
|
$foreign = Str::singular($table) . '_id'; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
// Define the Column |
|
29
|
|
|
$column = $this->belongsToColumn($foreign); |
|
30
|
|
|
|
|
31
|
|
|
// Define the Relation |
|
32
|
|
|
$this->belongsToForeign($table, $foreign, $local, $name, $onUpdate, $onDelete); |
|
33
|
|
|
|
|
34
|
|
|
// Return the Column |
|
35
|
|
|
return $column; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Defines a Belongs To Column and Relationship using a Big Integer. |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $table |
|
42
|
|
|
* @param string|null $foreign |
|
43
|
|
|
* @param string|null $local |
|
44
|
|
|
* @param string|null $name |
|
45
|
|
|
* @param string|null $onUpdate |
|
46
|
|
|
* @param string|null $onDelete |
|
47
|
|
|
* |
|
48
|
|
|
* @return \Illuminate\Support\Fluent |
|
49
|
|
|
*/ |
|
50
|
|
|
public function bigBelongsTo($table, $foreign = null, $local = null, $name = null, $onUpdate = null, $onDelete = null) |
|
51
|
|
|
{ |
|
52
|
|
|
// Determine the Name of the Foreign Key |
|
53
|
|
|
if(is_null($foreign)) { |
|
54
|
|
|
$foreign = Str::singular($table) . '_id'; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
// Define the Column |
|
58
|
|
|
$column = $this->bigBelongsToColumn($foreign); |
|
59
|
|
|
|
|
60
|
|
|
// Define the Relation |
|
61
|
|
|
$this->belongsToForeign($table, $foreign, $local, $name, $onUpdate, $onDelete); |
|
62
|
|
|
|
|
63
|
|
|
// Return the Column |
|
64
|
|
|
return $column; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Defines a Belongs To Column. |
|
69
|
|
|
* |
|
70
|
|
|
* @param string $foreign |
|
71
|
|
|
* @param boolean $big |
|
72
|
|
|
* |
|
73
|
|
|
* @return \Illuminate\Support\Fluent |
|
74
|
|
|
*/ |
|
75
|
|
|
public function belongsToColumn($foreign, $big = false) |
|
76
|
|
|
{ |
|
77
|
|
|
// Determine the Column Method |
|
78
|
|
|
$method = $big ? 'bigInteger' : 'integer'; |
|
79
|
|
|
|
|
80
|
|
|
// Define the Column |
|
81
|
|
|
return $this->{$method}($foreign)->unsigned(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Defines a Belongs To Column using a Big Integer. |
|
86
|
|
|
* |
|
87
|
|
|
* @param string $foreign |
|
88
|
|
|
* |
|
89
|
|
|
* @return \Illuminate\Support\Fluent |
|
90
|
|
|
*/ |
|
91
|
|
|
public function bigBelongsToColumn($foreign) |
|
92
|
|
|
{ |
|
93
|
|
|
return $this->belongsToColumn($foreign, true); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Defines a Belongs To Foreign Key. |
|
98
|
|
|
* |
|
99
|
|
|
* @param string $table |
|
100
|
|
|
* @param string|null $foreign |
|
101
|
|
|
* @param string|null $local |
|
102
|
|
|
* @param string|null $name |
|
103
|
|
|
* @param string|null $onUpdate |
|
104
|
|
|
* @param string|null $onDelete |
|
105
|
|
|
* |
|
106
|
|
|
* @return \Illuminate\Support\Fluent |
|
107
|
|
|
*/ |
|
108
|
|
|
public function belongsToForeign($table, $foreign = null, $local = null, $name = null, $onUpdate = null, $onDelete = null) |
|
109
|
|
|
{ |
|
110
|
|
|
// Determine the Name of the Foreign Key |
|
111
|
|
|
if(is_null($foreign)) { |
|
112
|
|
|
$foreign = Str::singular($table) . '_id'; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
// Determine the Name of the Local Key |
|
116
|
|
|
if(is_null($local)) { |
|
117
|
|
|
$local = 'id'; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
// Determine the Update Constraint |
|
121
|
|
|
if(is_null($onUpdate)) { |
|
122
|
|
|
$onUpdate = $this->getConfig('belongsTo.onUpdate'); |
|
|
|
|
|
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
// Determine the Delete Constraint |
|
126
|
|
|
if(is_null($onDelete)) { |
|
127
|
|
|
$onDelete = $this->getConfig('belongsTo.onDelete'); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
// Define the Reference |
|
131
|
|
|
return $this->foreign($foreign, $name, $table)->references($local)->onUpdate($onUpdate)->onDelete($onDelete); |
|
|
|
|
|
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Drops a Belongs To Relationship. |
|
136
|
|
|
* |
|
137
|
|
|
* @param string $table |
|
138
|
|
|
* @param string|null $foreign |
|
139
|
|
|
* @param string|null $local |
|
140
|
|
|
* @param string|null $name |
|
141
|
|
|
* |
|
142
|
|
|
* @return \Illuminate\Support\Fluent |
|
143
|
|
|
*/ |
|
144
|
|
|
public function dropBelongsTo($table, $foreign = null, $local = null, $name = null) |
|
145
|
|
|
{ |
|
146
|
|
|
// Determine the Name of the Foreign Key |
|
147
|
|
|
if(is_null($foreign)) { |
|
148
|
|
|
$foreign = Str::singular($table) . '_id'; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
// Drop the Relation |
|
152
|
|
|
$this->dropBelongsToForeign($table, $foreign, $local, $name); |
|
153
|
|
|
|
|
154
|
|
|
// Drop the Column |
|
155
|
|
|
return $this->dropBelongsToColumn($foreign); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Drops a Belongs To Column. |
|
160
|
|
|
* |
|
161
|
|
|
* @param string $foreign |
|
162
|
|
|
* |
|
163
|
|
|
* @return \Illuminate\Support\Fluent |
|
164
|
|
|
*/ |
|
165
|
|
|
public function dropBelongsToColumn($foreign) |
|
166
|
|
|
{ |
|
167
|
|
|
$this->dropColumn($foreign); |
|
|
|
|
|
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Drops a Belongs To Foreign Key. |
|
172
|
|
|
* |
|
173
|
|
|
* @param string $table |
|
174
|
|
|
* @param string|null $foreign |
|
175
|
|
|
* @param string|null $local |
|
176
|
|
|
* @param string|null $name |
|
177
|
|
|
* |
|
178
|
|
|
* @return \Illuminate\Support\Fluent |
|
179
|
|
|
*/ |
|
180
|
|
|
public function dropBelongsToForeign($table, $foreign = null, $local = null, $name = null) |
|
181
|
|
|
{ |
|
182
|
|
|
// Check for a Name |
|
183
|
|
|
if(!is_null($name)) { |
|
184
|
|
|
return $this->dropForeign($name); |
|
|
|
|
|
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
// Determine the Name of the Foreign Key |
|
188
|
|
|
if(is_null($foreign)) { |
|
189
|
|
|
$foreign = Str::singular($table) . '_id'; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
// Determine the Name of the Local Key |
|
193
|
|
|
if(is_null($local)) { |
|
194
|
|
|
$local = 'id'; |
|
|
|
|
|
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
// Determine the Name of the Foreign Key |
|
198
|
|
|
$name = $this->createIndexName('foreign', [$foreign], $table); |
|
|
|
|
|
|
199
|
|
|
|
|
200
|
|
|
// Drop the Reference |
|
201
|
|
|
return $this->dropForeign($name); |
|
202
|
|
|
} |
|
203
|
|
|
} |