1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Reedware\LaravelBlueprints\Concerns; |
4
|
|
|
|
5
|
|
|
trait TimestampsBy |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Adds the 'created_by_id' and 'updated_by_id' References. |
9
|
|
|
* |
10
|
|
|
* @param string|null $table |
11
|
|
|
* @param string|null $local |
12
|
|
|
* @param string|null $name |
13
|
|
|
* @param string|null $onUpdate |
14
|
|
|
* @param string|null $onDelete |
15
|
|
|
* |
16
|
|
|
* @return void |
17
|
|
|
*/ |
18
|
|
|
public function timestampsBy($table = null, $local = null, $name = null, $onUpdate = null, $onDelete = null) |
19
|
|
|
{ |
20
|
|
|
// Add the 'created_by_id' Reference |
21
|
|
|
$this->createdBy($table, $local, $name, $onUpdate, $onDelete); |
22
|
|
|
|
23
|
|
|
// Add the 'updated_by_id' Reference |
24
|
|
|
$this->updatedBy($table, $local, $name, $onUpdate, $onDelete); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Adds the 'created_by_id' and 'updated_by_id' Morph References. |
29
|
|
|
* |
30
|
|
|
* @param string|null $indexName |
31
|
|
|
* |
32
|
|
|
* @return void |
33
|
|
|
*/ |
34
|
|
|
public function timestampsByMorph($indexName = null) |
35
|
|
|
{ |
36
|
|
|
// Add the 'created_by_id' Reference |
37
|
|
|
$this->createdByMorph($indexName); |
38
|
|
|
|
39
|
|
|
// Add the 'updated_by_id' Reference |
40
|
|
|
$this->updatedByMorph($indexName); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Adds the 'created_by_id' and 'updated_by_id' References. |
45
|
|
|
* |
46
|
|
|
* @param string $table |
47
|
|
|
* @param string|null $local |
48
|
|
|
* @param string|null $name |
49
|
|
|
* @param string|null $onUpdate |
50
|
|
|
* @param string|null $onDelete |
51
|
|
|
* |
52
|
|
|
* @return void |
53
|
|
|
*/ |
54
|
|
|
public function bigTimestampsBy($table = null, $local = null, $name = null, $onUpdate = null, $onDelete = null) |
55
|
|
|
{ |
56
|
|
|
// Add the 'created_by_id' Reference |
57
|
|
|
$this->bigCreatedBy($table, $local, $name, $onUpdate, $onDelete); |
58
|
|
|
|
59
|
|
|
// Add the 'updated_by_id' Reference |
60
|
|
|
$this->bigUpdatedBy($table, $local, $name, $onUpdate, $onDelete); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Adds the 'created_by_id' and 'updated_by_id' Morph References. |
65
|
|
|
* |
66
|
|
|
* @param string|null $indexName |
67
|
|
|
* |
68
|
|
|
* @return void |
69
|
|
|
*/ |
70
|
|
|
public function bigTimestampsByMorph($indexName = null) |
71
|
|
|
{ |
72
|
|
|
// Add the 'created_by_id' Reference |
73
|
|
|
$this->bigCreatedByMorph($indexName); |
74
|
|
|
|
75
|
|
|
// Add the 'updated_by_id' Reference |
76
|
|
|
$this->bigUpdatedByMorph($indexName); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Adds the 'created_by_id' Reference. |
81
|
|
|
* |
82
|
|
|
* @param string $table |
83
|
|
|
* @param string|null $local |
84
|
|
|
* @param string|null $name |
85
|
|
|
* @param string|null $onUpdate |
86
|
|
|
* @param string|null $onDelete |
87
|
|
|
* |
88
|
|
|
* @return \Illuminate\Support\Fluent |
89
|
|
|
*/ |
90
|
|
|
public function createdBy($table = null, $local = null, $name = null, $onUpdate = null, $onDelete = null) |
91
|
|
|
{ |
92
|
|
|
// Determine the Table |
93
|
|
|
if(is_null($table)) { |
94
|
|
|
$table = $this->getTimestampsByTable(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
// Create the Belongs to Relation |
98
|
|
|
return $this->belongsTo($table, 'created_by_id', $local, $name, $onUpdate, $onDelete)->nullable(); |
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Adds the 'created_by_id' Morph Reference. |
103
|
|
|
* |
104
|
|
|
* @param string $indexName |
105
|
|
|
* |
106
|
|
|
* @return \Illuminate\Support\Fluent |
107
|
|
|
*/ |
108
|
|
|
public function createdByMorph($indexName = null) |
109
|
|
|
{ |
110
|
|
|
// Create the 'created_by_id' Morph Column |
111
|
|
|
$this->belongsToColumn('created_by_id')->nullable(); |
|
|
|
|
112
|
|
|
|
113
|
|
|
// Create the 'created_by_type' Morph Column |
114
|
|
|
$this->string('created_by_type', 255)->nullable(); |
|
|
|
|
115
|
|
|
|
116
|
|
|
// Index the two Columns |
117
|
|
|
$this->index(['created_by_id', 'created_by_type'], $indexName); |
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Adds the 'created_by_id' Reference. |
122
|
|
|
* |
123
|
|
|
* @param string $table |
124
|
|
|
* @param string|null $local |
125
|
|
|
* @param string|null $name |
126
|
|
|
* @param string|null $onUpdate |
127
|
|
|
* @param string|null $onDelete |
128
|
|
|
* |
129
|
|
|
* @return \Illuminate\Support\Fluent |
130
|
|
|
*/ |
131
|
|
|
public function bigCreatedBy($table = null, $local = null, $name = null, $onUpdate = null, $onDelete = null) |
132
|
|
|
{ |
133
|
|
|
// Determine the Table |
134
|
|
|
if(is_null($table)) { |
135
|
|
|
$table = $this->getTimestampsByTable(); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
// Create the Belongs to Relation |
139
|
|
|
return $this->bigBelongsTo($table, 'created_by_id', $local, $name, $onUpdate, $onDelete)->nullable(); |
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Adds the 'created_by_id' Morph Reference. |
144
|
|
|
* |
145
|
|
|
* @param string $indexName |
146
|
|
|
* |
147
|
|
|
* @return \Illuminate\Support\Fluent |
148
|
|
|
*/ |
149
|
|
|
public function bigCreatedByMorph($indexName = null) |
150
|
|
|
{ |
151
|
|
|
// Create the 'created_by_id' Morph Column |
152
|
|
|
$this->bigBelongsToColumn('created_by_id')->nullable(); |
|
|
|
|
153
|
|
|
|
154
|
|
|
// Create the 'created_by_type' Morph Column |
155
|
|
|
$this->string('created_by_type', 255)->nullable(); |
156
|
|
|
|
157
|
|
|
// Index the two Columns |
158
|
|
|
$this->index(['created_by_id', 'created_by_type'], $indexName); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Adds the 'updated_by_id' Reference. |
163
|
|
|
* |
164
|
|
|
* @param string $table |
165
|
|
|
* @param string|null $local |
166
|
|
|
* @param string|null $name |
167
|
|
|
* @param string|null $onUpdate |
168
|
|
|
* @param string|null $onDelete |
169
|
|
|
* |
170
|
|
|
* @return \Illuminate\Support\Fluent |
171
|
|
|
*/ |
172
|
|
|
public function updatedBy($table = null, $local = null, $name = null, $onUpdate = null, $onDelete = null) |
173
|
|
|
{ |
174
|
|
|
// Determine the Table |
175
|
|
|
if(is_null($table)) { |
176
|
|
|
$table = $this->getTimestampsByTable(); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
// Create the Belongs to Relation |
180
|
|
|
return $this->belongsTo($table, 'updated_by_id', $local, $name, $onUpdate, $onDelete)->nullable(); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Adds the 'updated_by_id' Morph Reference. |
185
|
|
|
* |
186
|
|
|
* @param string $indexName |
187
|
|
|
* |
188
|
|
|
* @return \Illuminate\Support\Fluent |
189
|
|
|
*/ |
190
|
|
|
public function updatedByMorph($indexName = null) |
191
|
|
|
{ |
192
|
|
|
// Create the 'updated_by_id' Morph Column |
193
|
|
|
$this->belongsToColumn('updated_by_id')->nullable(); |
194
|
|
|
|
195
|
|
|
// Create the 'updated_by_type' Morph Column |
196
|
|
|
$this->string('updated_by_type', 255)->nullable(); |
197
|
|
|
|
198
|
|
|
// Index the two Columns |
199
|
|
|
$this->index(['updated_by_id', 'updated_by_type'], $indexName); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Adds the 'updated_by_id' Reference. |
204
|
|
|
* |
205
|
|
|
* @param string $table |
206
|
|
|
* @param string|null $local |
207
|
|
|
* @param string|null $name |
208
|
|
|
* @param string|null $onUpdate |
209
|
|
|
* @param string|null $onDelete |
210
|
|
|
* |
211
|
|
|
* @return \Illuminate\Support\Fluent |
212
|
|
|
*/ |
213
|
|
|
public function bigUpdatedBy($table = null, $local = null, $name = null, $onUpdate = null, $onDelete = null) |
214
|
|
|
{ |
215
|
|
|
// Determine the Table |
216
|
|
|
if(is_null($table)) { |
217
|
|
|
$table = $this->getTimestampsByTable(); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
// Create the Belongs to Relation |
221
|
|
|
return $this->bigBelongsTo($table, 'updated_by_id', $local, $name, $onUpdate, $onDelete)->nullable(); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Adds the 'updated_by_id' Morph Reference. |
226
|
|
|
* |
227
|
|
|
* @param string $indexName |
228
|
|
|
* |
229
|
|
|
* @return \Illuminate\Support\Fluent |
230
|
|
|
*/ |
231
|
|
|
public function bigUpdatedByMorph($indexName = null) |
232
|
|
|
{ |
233
|
|
|
// Create the 'updated_by_id' Morph Column |
234
|
|
|
$this->bigBelongsToColumn('updated_by_id')->nullable(); |
235
|
|
|
|
236
|
|
|
// Create the 'updated_by_type' Morph Column |
237
|
|
|
$this->string('updated_by_type', 255)->nullable(); |
238
|
|
|
|
239
|
|
|
// Index the two Columns |
240
|
|
|
$this->index(['updated_by_id', 'updated_by_type'], $indexName); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Returns the Table used for Timestamps By Relations. |
245
|
|
|
* |
246
|
|
|
* @return string |
247
|
|
|
*/ |
248
|
|
|
public function getTimestampsByTable() |
249
|
|
|
{ |
250
|
|
|
// Determine the default Timestamps By Model |
251
|
|
|
$model = $this->getConfig('timestampsBy.model'); |
|
|
|
|
252
|
|
|
|
253
|
|
|
// Create a new Model instance |
254
|
|
|
$instance = new $model; |
255
|
|
|
|
256
|
|
|
// Return the Table |
257
|
|
|
return $instance->getTable(); |
258
|
|
|
} |
259
|
|
|
} |