1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Staudenmeir\LaravelAdjacencyList\Eloquent\Traits; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
use Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\BelongsToManyOfDescendants; |
9
|
|
|
use Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\HasManyOfDescendants; |
10
|
|
|
use Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\MorphToManyOfDescendants; |
11
|
|
|
|
12
|
|
|
trait HasOfDescendantsRelationships |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Define a one-to-many relationship of the model's descendants. |
16
|
|
|
* |
17
|
|
|
* @param string $related |
18
|
|
|
* @param string|null $foreignKey |
19
|
|
|
* @param string|null $localKey |
20
|
|
|
* @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\HasManyOfDescendants<static> |
21
|
|
|
*/ |
22
|
70 |
|
public function hasManyOfDescendants($related, $foreignKey = null, $localKey = null) |
23
|
|
|
{ |
24
|
70 |
|
$instance = $this->newRelatedInstance($related); |
|
|
|
|
25
|
|
|
|
26
|
70 |
|
$foreignKey = $foreignKey ?: $this->getForeignKey(); |
|
|
|
|
27
|
|
|
|
28
|
70 |
|
$localKey = $localKey ?: $this->getKeyName(); |
|
|
|
|
29
|
|
|
|
30
|
70 |
|
return $this->newHasManyOfDescendants( |
31
|
70 |
|
$instance->newQuery(), |
32
|
70 |
|
$this, |
|
|
|
|
33
|
70 |
|
$instance->qualifyColumn($foreignKey), |
34
|
70 |
|
$localKey, |
35
|
70 |
|
false |
36
|
70 |
|
); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Define a one-to-many relationship of the model's descendants and itself. |
41
|
|
|
* |
42
|
|
|
* @param string $related |
43
|
|
|
* @param string|null $foreignKey |
44
|
|
|
* @param string|null $localKey |
45
|
|
|
* @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\HasManyOfDescendants<static> |
46
|
|
|
*/ |
47
|
25 |
|
public function hasManyOfDescendantsAndSelf($related, $foreignKey = null, $localKey = null) |
48
|
|
|
{ |
49
|
25 |
|
$instance = $this->newRelatedInstance($related); |
50
|
|
|
|
51
|
25 |
|
$foreignKey = $foreignKey ?: $this->getForeignKey(); |
52
|
|
|
|
53
|
25 |
|
$localKey = $localKey ?: $this->getKeyName(); |
54
|
|
|
|
55
|
25 |
|
return $this->newHasManyOfDescendants( |
56
|
25 |
|
$instance->newQuery(), |
57
|
25 |
|
$this, |
|
|
|
|
58
|
25 |
|
$instance->qualifyColumn($foreignKey), |
59
|
25 |
|
$localKey, |
60
|
25 |
|
true |
61
|
25 |
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Instantiate a new HasManyOfDescendants relationship. |
66
|
|
|
* |
67
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
68
|
|
|
* @param \Illuminate\Database\Eloquent\Model $parent |
69
|
|
|
* @param string $foreignKey |
70
|
|
|
* @param string $localKey |
71
|
|
|
* @param bool $andSelf |
72
|
|
|
* @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\HasManyOfDescendants<static> |
73
|
|
|
*/ |
74
|
95 |
|
protected function newHasManyOfDescendants(Builder $query, Model $parent, $foreignKey, $localKey, $andSelf) |
75
|
|
|
{ |
76
|
95 |
|
return new HasManyOfDescendants($query, $parent, $foreignKey, $localKey, $andSelf); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Define a many-to-many relationship of the model's descendants. |
81
|
|
|
* |
82
|
|
|
* @param string $related |
83
|
|
|
* @param string|null $table |
84
|
|
|
* @param string|null $foreignPivotKey |
85
|
|
|
* @param string|null $relatedPivotKey |
86
|
|
|
* @param string|null $parentKey |
87
|
|
|
* @param string|null $relatedKey |
88
|
|
|
* @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\BelongsToManyOfDescendants<static> |
89
|
|
|
*/ |
90
|
60 |
|
public function belongsToManyOfDescendants( |
91
|
|
|
$related, |
92
|
|
|
$table = null, |
93
|
|
|
$foreignPivotKey = null, |
94
|
|
|
$relatedPivotKey = null, |
95
|
|
|
$parentKey = null, |
96
|
|
|
$relatedKey = null |
97
|
|
|
) { |
98
|
60 |
|
$instance = $this->newRelatedInstance($related); |
99
|
|
|
|
100
|
60 |
|
$foreignPivotKey = $foreignPivotKey ?: $this->getForeignKey(); |
101
|
|
|
|
102
|
60 |
|
$relatedPivotKey = $relatedPivotKey ?: $instance->getForeignKey(); |
103
|
|
|
|
104
|
60 |
|
if (is_null($table)) { |
105
|
60 |
|
$table = $this->joiningTable($related, $instance); |
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
108
|
60 |
|
return $this->newBelongsToManyOfDescendants( |
109
|
60 |
|
$instance->newQuery(), |
110
|
60 |
|
$this, |
|
|
|
|
111
|
60 |
|
$table, |
112
|
60 |
|
$foreignPivotKey, |
113
|
60 |
|
$relatedPivotKey, |
114
|
60 |
|
$parentKey ?: $this->getKeyName(), |
115
|
60 |
|
$relatedKey ?: $instance->getKeyName(), |
116
|
60 |
|
false |
117
|
60 |
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Define a many-to-many relationship of the model's descendants and itself. |
122
|
|
|
* |
123
|
|
|
* @param string $related |
124
|
|
|
* @param string|null $table |
125
|
|
|
* @param string|null $foreignPivotKey |
126
|
|
|
* @param string|null $relatedPivotKey |
127
|
|
|
* @param string|null $parentKey |
128
|
|
|
* @param string|null $relatedKey |
129
|
|
|
* @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\BelongsToManyOfDescendants<static> |
130
|
|
|
*/ |
131
|
25 |
|
public function belongsToManyOfDescendantsAndSelf( |
132
|
|
|
$related, |
133
|
|
|
$table = null, |
134
|
|
|
$foreignPivotKey = null, |
135
|
|
|
$relatedPivotKey = null, |
136
|
|
|
$parentKey = null, |
137
|
|
|
$relatedKey = null |
138
|
|
|
) { |
139
|
25 |
|
$instance = $this->newRelatedInstance($related); |
140
|
|
|
|
141
|
25 |
|
$foreignPivotKey = $foreignPivotKey ?: $this->getForeignKey(); |
142
|
|
|
|
143
|
25 |
|
$relatedPivotKey = $relatedPivotKey ?: $instance->getForeignKey(); |
144
|
|
|
|
145
|
25 |
|
if (is_null($table)) { |
146
|
25 |
|
$table = $this->joiningTable($related, $instance); |
147
|
|
|
} |
148
|
|
|
|
149
|
25 |
|
return $this->newBelongsToManyOfDescendants( |
150
|
25 |
|
$instance->newQuery(), |
151
|
25 |
|
$this, |
|
|
|
|
152
|
25 |
|
$table, |
153
|
25 |
|
$foreignPivotKey, |
154
|
25 |
|
$relatedPivotKey, |
155
|
25 |
|
$parentKey ?: $this->getKeyName(), |
156
|
25 |
|
$relatedKey ?: $instance->getKeyName(), |
157
|
25 |
|
true |
158
|
25 |
|
); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Instantiate a new BelongsToManyOfDescendants relationship. |
163
|
|
|
* |
164
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
165
|
|
|
* @param \Illuminate\Database\Eloquent\Model $parent |
166
|
|
|
* @param string $table |
167
|
|
|
* @param string $foreignPivotKey |
168
|
|
|
* @param string $relatedPivotKey |
169
|
|
|
* @param string $parentKey |
170
|
|
|
* @param string $relatedKey |
171
|
|
|
* @param bool $andSelf |
172
|
|
|
* @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\BelongsToManyOfDescendants<static> |
173
|
|
|
*/ |
174
|
85 |
|
protected function newBelongsToManyOfDescendants( |
175
|
|
|
Builder $query, |
176
|
|
|
Model $parent, |
177
|
|
|
$table, |
178
|
|
|
$foreignPivotKey, |
179
|
|
|
$relatedPivotKey, |
180
|
|
|
$parentKey, |
181
|
|
|
$relatedKey, |
182
|
|
|
$andSelf |
183
|
|
|
) { |
184
|
85 |
|
return new BelongsToManyOfDescendants( |
185
|
85 |
|
$query, |
186
|
85 |
|
$parent, |
187
|
85 |
|
$table, |
188
|
85 |
|
$foreignPivotKey, |
189
|
85 |
|
$relatedPivotKey, |
190
|
85 |
|
$parentKey, |
191
|
85 |
|
$relatedKey, |
192
|
85 |
|
$andSelf |
193
|
85 |
|
); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Define a polymorphic many-to-many relationship of the model's descendants. |
198
|
|
|
* |
199
|
|
|
* @param string $related |
200
|
|
|
* @param string $name |
201
|
|
|
* @param string|null $table |
202
|
|
|
* @param string|null $foreignPivotKey |
203
|
|
|
* @param string|null $relatedPivotKey |
204
|
|
|
* @param string|null $parentKey |
205
|
|
|
* @param string|null $relatedKey |
206
|
|
|
* @param bool $inverse |
207
|
|
|
* @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\MorphToManyOfDescendants<static> |
208
|
|
|
*/ |
209
|
120 |
|
public function morphToManyOfDescendants( |
210
|
|
|
$related, |
211
|
|
|
$name, |
212
|
|
|
$table = null, |
213
|
|
|
$foreignPivotKey = null, |
214
|
|
|
$relatedPivotKey = null, |
215
|
|
|
$parentKey = null, |
216
|
|
|
$relatedKey = null, |
217
|
|
|
$inverse = false |
218
|
|
|
) { |
219
|
120 |
|
$instance = $this->newRelatedInstance($related); |
220
|
|
|
|
221
|
120 |
|
$foreignPivotKey = $foreignPivotKey ?: $name.'_id'; |
222
|
|
|
|
223
|
120 |
|
$relatedPivotKey = $relatedPivotKey ?: $instance->getForeignKey(); |
224
|
|
|
|
225
|
120 |
|
if (!$table) { |
226
|
120 |
|
$words = preg_split('/(_)/u', $name, -1, PREG_SPLIT_DELIM_CAPTURE); |
227
|
|
|
|
228
|
120 |
|
$lastWord = array_pop($words); |
229
|
|
|
|
230
|
120 |
|
$table = implode('', $words).Str::plural($lastWord); |
231
|
|
|
} |
232
|
|
|
|
233
|
120 |
|
return $this->newMorphToManyOfDescendants( |
234
|
120 |
|
$instance->newQuery(), |
235
|
120 |
|
$this, |
|
|
|
|
236
|
120 |
|
$name, |
237
|
120 |
|
$table, |
238
|
120 |
|
$foreignPivotKey, |
239
|
120 |
|
$relatedPivotKey, |
240
|
120 |
|
$parentKey ?: $this->getKeyName(), |
241
|
120 |
|
$relatedKey ?: $instance->getKeyName(), |
242
|
120 |
|
$inverse, |
243
|
120 |
|
false |
244
|
120 |
|
); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Define a polymorphic many-to-many relationship of the model's descendants and itself. |
249
|
|
|
* |
250
|
|
|
* @param string $related |
251
|
|
|
* @param string $name |
252
|
|
|
* @param string|null $table |
253
|
|
|
* @param string|null $foreignPivotKey |
254
|
|
|
* @param string|null $relatedPivotKey |
255
|
|
|
* @param string|null $parentKey |
256
|
|
|
* @param string|null $relatedKey |
257
|
|
|
* @param bool $inverse |
258
|
|
|
* @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\MorphToManyOfDescendants<static> |
259
|
|
|
*/ |
260
|
50 |
|
public function morphToManyOfDescendantsAndSelf( |
261
|
|
|
$related, |
262
|
|
|
$name, |
263
|
|
|
$table = null, |
264
|
|
|
$foreignPivotKey = null, |
265
|
|
|
$relatedPivotKey = null, |
266
|
|
|
$parentKey = null, |
267
|
|
|
$relatedKey = null, |
268
|
|
|
$inverse = false |
269
|
|
|
) { |
270
|
50 |
|
$instance = $this->newRelatedInstance($related); |
271
|
|
|
|
272
|
50 |
|
$foreignPivotKey = $foreignPivotKey ?: $name.'_id'; |
273
|
|
|
|
274
|
50 |
|
$relatedPivotKey = $relatedPivotKey ?: $instance->getForeignKey(); |
275
|
|
|
|
276
|
50 |
|
if (!$table) { |
277
|
50 |
|
$words = preg_split('/(_)/u', $name, -1, PREG_SPLIT_DELIM_CAPTURE); |
278
|
|
|
|
279
|
50 |
|
$lastWord = array_pop($words); |
280
|
|
|
|
281
|
50 |
|
$table = implode('', $words).Str::plural($lastWord); |
282
|
|
|
} |
283
|
|
|
|
284
|
50 |
|
return $this->newMorphToManyOfDescendants( |
285
|
50 |
|
$instance->newQuery(), |
286
|
50 |
|
$this, |
|
|
|
|
287
|
50 |
|
$name, |
288
|
50 |
|
$table, |
289
|
50 |
|
$foreignPivotKey, |
290
|
50 |
|
$relatedPivotKey, |
291
|
50 |
|
$parentKey ?: $this->getKeyName(), |
292
|
50 |
|
$relatedKey ?: $instance->getKeyName(), |
293
|
50 |
|
$inverse, |
294
|
50 |
|
true |
295
|
50 |
|
); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Instantiate a new MorphToManyOfDescendants relationship. |
300
|
|
|
* |
301
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
302
|
|
|
* @param \Illuminate\Database\Eloquent\Model $parent |
303
|
|
|
* @param string $name |
304
|
|
|
* @param string $table |
305
|
|
|
* @param string $foreignPivotKey |
306
|
|
|
* @param string $relatedPivotKey |
307
|
|
|
* @param string $parentKey |
308
|
|
|
* @param string $relatedKey |
309
|
|
|
* @param bool $inverse |
310
|
|
|
* @param bool $andSelf |
311
|
|
|
* @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\MorphToManyOfDescendants<static> |
312
|
|
|
*/ |
313
|
170 |
|
protected function newMorphToManyOfDescendants( |
314
|
|
|
Builder $query, |
315
|
|
|
Model $parent, |
316
|
|
|
$name, |
317
|
|
|
$table, |
318
|
|
|
$foreignPivotKey, |
319
|
|
|
$relatedPivotKey, |
320
|
|
|
$parentKey, |
321
|
|
|
$relatedKey, |
322
|
|
|
$inverse, |
323
|
|
|
$andSelf |
324
|
|
|
) { |
325
|
170 |
|
return new MorphToManyOfDescendants( |
326
|
170 |
|
$query, |
327
|
170 |
|
$parent, |
328
|
170 |
|
$name, |
329
|
170 |
|
$table, |
330
|
170 |
|
$foreignPivotKey, |
331
|
170 |
|
$relatedPivotKey, |
332
|
170 |
|
$parentKey, |
333
|
170 |
|
$relatedKey, |
334
|
170 |
|
$inverse, |
335
|
170 |
|
$andSelf |
336
|
170 |
|
); |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* Define a polymorphic, inverse many-to-many relationship of the model's descendants. |
341
|
|
|
* |
342
|
|
|
* @param string $related |
343
|
|
|
* @param string $name |
344
|
|
|
* @param string|null $table |
345
|
|
|
* @param string|null $foreignPivotKey |
346
|
|
|
* @param string|null $relatedPivotKey |
347
|
|
|
* @param string|null $parentKey |
348
|
|
|
* @param string|null $relatedKey |
349
|
|
|
* @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\MorphToManyOfDescendants<static> |
350
|
|
|
*/ |
351
|
60 |
|
public function morphedByManyOfDescendants( |
352
|
|
|
$related, |
353
|
|
|
$name, |
354
|
|
|
$table = null, |
355
|
|
|
$foreignPivotKey = null, |
356
|
|
|
$relatedPivotKey = null, |
357
|
|
|
$parentKey = null, |
358
|
|
|
$relatedKey = null |
359
|
|
|
) { |
360
|
60 |
|
$foreignPivotKey = $foreignPivotKey ?: $this->getForeignKey(); |
361
|
|
|
|
362
|
60 |
|
$relatedPivotKey = $relatedPivotKey ?: $name.'_id'; |
363
|
|
|
|
364
|
60 |
|
return $this->morphToManyOfDescendants( |
365
|
60 |
|
$related, |
366
|
60 |
|
$name, |
367
|
60 |
|
$table, |
368
|
60 |
|
$foreignPivotKey, |
369
|
60 |
|
$relatedPivotKey, |
370
|
60 |
|
$parentKey, |
371
|
60 |
|
$relatedKey, |
372
|
60 |
|
true |
373
|
60 |
|
); |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
/** |
377
|
|
|
* Define a polymorphic, inverse many-to-many relationship of the model's descendants and itself. |
378
|
|
|
* |
379
|
|
|
* @param string $related |
380
|
|
|
* @param string $name |
381
|
|
|
* @param string|null $table |
382
|
|
|
* @param string|null $foreignPivotKey |
383
|
|
|
* @param string|null $relatedPivotKey |
384
|
|
|
* @param string|null $parentKey |
385
|
|
|
* @param string|null $relatedKey |
386
|
|
|
* @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\MorphToManyOfDescendants<static> |
387
|
|
|
*/ |
388
|
25 |
|
public function morphedByManyOfDescendantsAndSelf( |
389
|
|
|
$related, |
390
|
|
|
$name, |
391
|
|
|
$table = null, |
392
|
|
|
$foreignPivotKey = null, |
393
|
|
|
$relatedPivotKey = null, |
394
|
|
|
$parentKey = null, |
395
|
|
|
$relatedKey = null |
396
|
|
|
) { |
397
|
25 |
|
$foreignPivotKey = $foreignPivotKey ?: $this->getForeignKey(); |
398
|
|
|
|
399
|
25 |
|
$relatedPivotKey = $relatedPivotKey ?: $name.'_id'; |
400
|
|
|
|
401
|
25 |
|
return $this->morphToManyOfDescendantsAndSelf( |
402
|
25 |
|
$related, |
403
|
25 |
|
$name, |
404
|
25 |
|
$table, |
405
|
25 |
|
$foreignPivotKey, |
406
|
25 |
|
$relatedPivotKey, |
407
|
25 |
|
$parentKey, |
408
|
25 |
|
$relatedKey, |
409
|
25 |
|
true |
410
|
25 |
|
); |
411
|
|
|
} |
412
|
|
|
} |
413
|
|
|
|