|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Volosyuk\SimpleEloquent\Relations; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Relations\Relation as BaseRelation; |
|
7
|
|
|
use Illuminate\Support\Arr; |
|
8
|
|
|
use Illuminate\Support\Str; |
|
9
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Include relations definitions to eloquent |
|
13
|
|
|
* |
|
14
|
|
|
* @package Volosyuk\SimpleEloquent |
|
15
|
|
|
*/ |
|
16
|
|
|
trait HasRelationships |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Define a one-to-one relationship. |
|
20
|
|
|
* |
|
21
|
|
|
* @param string $related |
|
22
|
|
|
* @param string $foreignKey |
|
23
|
|
|
* @param string $localKey |
|
24
|
|
|
* @return HasOne |
|
25
|
|
|
*/ |
|
26
|
2 |
|
public function hasOne($related, $foreignKey = null, $localKey = null) |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var Model $instance |
|
30
|
|
|
*/ |
|
31
|
2 |
|
$instance = $this->newRelatedInstance($related); |
|
|
|
|
|
|
32
|
|
|
|
|
33
|
2 |
|
$foreignKey = $foreignKey ?: $this->getForeignKey(); |
|
|
|
|
|
|
34
|
|
|
|
|
35
|
2 |
|
$localKey = $localKey ?: $this->getKeyName(); |
|
|
|
|
|
|
36
|
|
|
|
|
37
|
2 |
|
return $this->newHasOne($instance->newQuery(), $this, $instance->getTable().'.'.$foreignKey, $localKey); |
|
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Instantiate a new HasOne relationship. |
|
42
|
|
|
* |
|
43
|
|
|
* @param Builder $query |
|
44
|
|
|
* @param Model $parent |
|
45
|
|
|
* @param string $foreignKey |
|
46
|
|
|
* @param string $localKey |
|
47
|
|
|
* @return HasOne |
|
48
|
|
|
*/ |
|
49
|
2 |
|
protected function newHasOne(Builder $query, Model $parent, $foreignKey, $localKey) |
|
50
|
|
|
{ |
|
51
|
2 |
|
return new HasOne($query, $parent, $foreignKey, $localKey); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Define a polymorphic one-to-one relationship. |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $related |
|
58
|
|
|
* @param string $name |
|
59
|
|
|
* @param string $type |
|
60
|
|
|
* @param string $id |
|
61
|
|
|
* @param string $localKey |
|
62
|
|
|
* @return MorphOne |
|
63
|
|
|
*/ |
|
64
|
2 |
|
public function morphOne($related, $name, $type = null, $id = null, $localKey = null) |
|
65
|
|
|
{ |
|
66
|
|
|
/** |
|
67
|
|
|
* @var Model $instance |
|
68
|
|
|
*/ |
|
69
|
2 |
|
$instance = $this->newRelatedInstance($related); |
|
70
|
|
|
|
|
71
|
2 |
|
list($type, $id) = $this->getMorphs($name, $type, $id); |
|
|
|
|
|
|
72
|
|
|
|
|
73
|
2 |
|
$table = $instance->getTable(); |
|
74
|
|
|
|
|
75
|
2 |
|
$localKey = $localKey ?: $this->getKeyName(); |
|
76
|
|
|
|
|
77
|
2 |
|
return $this->newMorphOne($instance->newQuery(), $this, $table.'.'.$type, $table.'.'.$id, $localKey); |
|
|
|
|
|
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Instantiate a new MorphOne relationship. |
|
82
|
|
|
* |
|
83
|
|
|
* @param Builder $query |
|
84
|
|
|
* @param Model $parent |
|
85
|
|
|
* @param string $type |
|
86
|
|
|
* @param string $id |
|
87
|
|
|
* @param string $localKey |
|
88
|
|
|
* @return MorphOne |
|
89
|
|
|
*/ |
|
90
|
2 |
|
protected function newMorphOne(Builder $query, Model $parent, $type, $id, $localKey) |
|
91
|
|
|
{ |
|
92
|
2 |
|
return new MorphOne($query, $parent, $type, $id, $localKey); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Define an inverse one-to-one or many relationship. |
|
97
|
|
|
* |
|
98
|
|
|
* @param string $related |
|
99
|
|
|
* @param string $foreignKey |
|
100
|
|
|
* @param string $ownerKey |
|
101
|
|
|
* @param string $relation |
|
102
|
|
|
* @return BelongsTo |
|
103
|
|
|
*/ |
|
104
|
2 |
|
public function belongsTo($related, $foreignKey = null, $ownerKey = null, $relation = null) |
|
105
|
|
|
{ |
|
106
|
2 |
|
if (is_null($relation)) { |
|
107
|
2 |
|
$relation = $this->guessBelongsToRelation(); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @var Model $instance |
|
112
|
|
|
*/ |
|
113
|
2 |
|
$instance = $this->newRelatedInstance($related); |
|
114
|
|
|
|
|
115
|
2 |
|
if (is_null($foreignKey)) { |
|
116
|
2 |
|
$foreignKey = Str::snake($relation).'_'.$instance->getKeyName(); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
2 |
|
$ownerKey = $ownerKey ?: $instance->getKeyName(); |
|
120
|
|
|
|
|
121
|
2 |
|
return $this->newBelongsTo( |
|
122
|
2 |
|
$instance->newQuery(), $this, $foreignKey, $ownerKey, $relation |
|
|
|
|
|
|
123
|
|
|
); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Instantiate a new BelongsTo relationship. |
|
128
|
|
|
* |
|
129
|
|
|
* @param Builder $query |
|
130
|
|
|
* @param Model $child |
|
131
|
|
|
* @param string $foreignKey |
|
132
|
|
|
* @param string $ownerKey |
|
133
|
|
|
* @param string $relation |
|
134
|
|
|
* @return BelongsTo |
|
135
|
|
|
*/ |
|
136
|
2 |
|
protected function newBelongsTo(Builder $query, Model $child, $foreignKey, $ownerKey, $relation) |
|
137
|
|
|
{ |
|
138
|
2 |
|
return new BelongsTo($query, $child, $foreignKey, $ownerKey, $relation); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Define a polymorphic, inverse one-to-one or many relationship. |
|
143
|
|
|
* |
|
144
|
|
|
* @param string $name |
|
145
|
|
|
* @param string $type |
|
146
|
|
|
* @param string $id |
|
147
|
|
|
* @param string $ownerKey |
|
148
|
|
|
* @return MorphTo |
|
149
|
|
|
*/ |
|
150
|
2 |
|
public function morphTo($name = null, $type = null, $id = null, $ownerKey = null) |
|
151
|
|
|
{ |
|
152
|
2 |
|
$name = $name ?: $this->guessBelongsToRelation(); |
|
153
|
|
|
|
|
154
|
2 |
|
list($type, $id) = $this->getMorphs( |
|
155
|
2 |
|
Str::snake($name), $type, $id |
|
156
|
|
|
); |
|
157
|
|
|
|
|
158
|
2 |
|
return empty($class = $this->{$type}) |
|
159
|
1 |
|
? $this->morphEagerTo($name, $type, $id, $ownerKey) |
|
160
|
2 |
|
: $this->morphInstanceTo($class, $name, $type, $id, $ownerKey); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Define a polymorphic, inverse one-to-one or many relationship. |
|
165
|
|
|
* |
|
166
|
|
|
* @param string $name |
|
167
|
|
|
* @param string $type |
|
168
|
|
|
* @param string $id |
|
169
|
|
|
* @param string $ownerKey |
|
170
|
|
|
* @return MorphTo |
|
171
|
|
|
*/ |
|
172
|
1 |
|
protected function morphEagerTo($name, $type, $id, $ownerKey) |
|
173
|
|
|
{ |
|
174
|
1 |
|
return $this->newMorphTo( |
|
175
|
1 |
|
$this->newQuery()->setEagerLoads([]), $this, $id, $ownerKey, $type, $name |
|
|
|
|
|
|
176
|
|
|
); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Define a polymorphic, inverse one-to-one or many relationship. |
|
181
|
|
|
* |
|
182
|
|
|
* @param string $target |
|
183
|
|
|
* @param string $name |
|
184
|
|
|
* @param string $type |
|
185
|
|
|
* @param string $id |
|
186
|
|
|
* @param string $ownerKey |
|
187
|
|
|
* @return MorphTo |
|
188
|
|
|
*/ |
|
189
|
2 |
|
protected function morphInstanceTo($target, $name, $type, $id, $ownerKey) |
|
190
|
|
|
{ |
|
191
|
|
|
/** |
|
192
|
|
|
* @var Model $instance |
|
193
|
|
|
*/ |
|
194
|
2 |
|
$instance = $this->newRelatedInstance( |
|
195
|
2 |
|
static::getActualClassNameForMorph($target) |
|
196
|
|
|
); |
|
197
|
|
|
|
|
198
|
2 |
|
return $this->newMorphTo( |
|
199
|
2 |
|
$instance->newQuery(), $this, $id, $ownerKey ?? $instance->getKeyName(), $type, $name |
|
|
|
|
|
|
200
|
|
|
); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* Instantiate a new MorphTo relationship. |
|
205
|
|
|
* |
|
206
|
|
|
* @param Builder $query |
|
207
|
|
|
* @param Model $parent |
|
208
|
|
|
* @param string $foreignKey |
|
209
|
|
|
* @param string $ownerKey |
|
210
|
|
|
* @param string $type |
|
211
|
|
|
* @param string $relation |
|
212
|
|
|
* @return MorphTo |
|
213
|
|
|
*/ |
|
214
|
2 |
|
protected function newMorphTo(Builder $query, Model $parent, $foreignKey, $ownerKey, $type, $relation) |
|
215
|
|
|
{ |
|
216
|
2 |
|
return new MorphTo($query, $parent, $foreignKey, $ownerKey, $type, $relation); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* Retrieve the actual class name for a given morph class. |
|
221
|
|
|
* |
|
222
|
|
|
* @param string $class |
|
223
|
|
|
* @return string |
|
224
|
|
|
*/ |
|
225
|
2 |
|
public static function getActualClassNameForMorph($class) |
|
226
|
|
|
{ |
|
227
|
2 |
|
return Arr::get(BaseRelation::morphMap() ?: [], $class, $class); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* Guess the "belongs to" relationship name. |
|
232
|
|
|
* |
|
233
|
|
|
* @return string |
|
234
|
|
|
*/ |
|
235
|
2 |
|
protected function guessBelongsToRelation() |
|
236
|
|
|
{ |
|
237
|
2 |
|
list($one, $two, $caller) = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3); |
|
238
|
|
|
|
|
239
|
2 |
|
return $caller['function']; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* Define a one-to-many relationship. |
|
244
|
|
|
* |
|
245
|
|
|
* @param string $related |
|
246
|
|
|
* @param string $foreignKey |
|
247
|
|
|
* @param string $localKey |
|
248
|
|
|
* @return HasMany |
|
249
|
|
|
*/ |
|
250
|
3 |
|
public function hasMany($related, $foreignKey = null, $localKey = null) |
|
251
|
|
|
{ |
|
252
|
|
|
/** |
|
253
|
|
|
* @var Model $instance |
|
254
|
|
|
*/ |
|
255
|
3 |
|
$instance = $this->newRelatedInstance($related); |
|
256
|
|
|
|
|
257
|
3 |
|
$foreignKey = $foreignKey ?: $this->getForeignKey(); |
|
258
|
|
|
|
|
259
|
3 |
|
$localKey = $localKey ?: $this->getKeyName(); |
|
260
|
|
|
|
|
261
|
3 |
|
return $this->newHasMany( |
|
262
|
3 |
|
$instance->newQuery(), $this, $instance->getTable().'.'.$foreignKey, $localKey |
|
|
|
|
|
|
263
|
|
|
); |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
/** |
|
267
|
|
|
* Instantiate a new HasMany relationship. |
|
268
|
|
|
* |
|
269
|
|
|
* @param Builder $query |
|
270
|
|
|
* @param Model $parent |
|
271
|
|
|
* @param string $foreignKey |
|
272
|
|
|
* @param string $localKey |
|
273
|
|
|
* @return HasMany |
|
274
|
|
|
*/ |
|
275
|
3 |
|
protected function newHasMany(Builder $query, Model $parent, $foreignKey, $localKey) |
|
276
|
|
|
{ |
|
277
|
3 |
|
return new HasMany($query, $parent, $foreignKey, $localKey); |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
/** |
|
281
|
|
|
* Define a has-many-through relationship. |
|
282
|
|
|
* |
|
283
|
|
|
* @param string $related |
|
284
|
|
|
* @param string $through |
|
285
|
|
|
* @param string|null $firstKey |
|
286
|
|
|
* @param string|null $secondKey |
|
287
|
|
|
* @param string|null $localKey |
|
288
|
|
|
* @param string|null $secondLocalKey |
|
289
|
|
|
* @return HasManyThrough |
|
290
|
|
|
*/ |
|
291
|
2 |
|
public function hasManyThrough($related, $through, $firstKey = null, $secondKey = null, $localKey = null, $secondLocalKey = null) |
|
292
|
|
|
{ |
|
293
|
|
|
/** |
|
294
|
|
|
* @var Model $through |
|
295
|
|
|
*/ |
|
296
|
2 |
|
$through = new $through; |
|
297
|
|
|
|
|
298
|
2 |
|
$firstKey = $firstKey ?: $this->getForeignKey(); |
|
299
|
|
|
|
|
300
|
2 |
|
$secondKey = $secondKey ?: $through->getForeignKey(); |
|
301
|
|
|
|
|
302
|
2 |
|
return $this->newHasManyThrough( |
|
303
|
2 |
|
$this->newRelatedInstance($related)->newQuery(), $this, $through, |
|
|
|
|
|
|
304
|
2 |
|
$firstKey, $secondKey, $localKey ?: $this->getKeyName(), |
|
305
|
2 |
|
$secondLocalKey ?: $through->getKeyName() |
|
306
|
|
|
); |
|
307
|
|
|
} |
|
308
|
|
|
|
|
309
|
|
|
/** |
|
310
|
|
|
* Instantiate a new HasManyThrough relationship. |
|
311
|
|
|
* |
|
312
|
|
|
* @param Builder $query |
|
313
|
|
|
* @param Model $farParent |
|
314
|
|
|
* @param Model $throughParent |
|
315
|
|
|
* @param string $firstKey |
|
316
|
|
|
* @param string $secondKey |
|
317
|
|
|
* @param string $localKey |
|
318
|
|
|
* @param string $secondLocalKey |
|
319
|
|
|
* @return HasManyThrough |
|
320
|
|
|
*/ |
|
321
|
2 |
|
protected function newHasManyThrough(Builder $query, Model $farParent, Model $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey) |
|
322
|
|
|
{ |
|
323
|
2 |
|
return new HasManyThrough($query, $farParent, $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey); |
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
|
|
/** |
|
327
|
|
|
* Define a polymorphic one-to-many relationship. |
|
328
|
|
|
* |
|
329
|
|
|
* @param string $related |
|
330
|
|
|
* @param string $name |
|
331
|
|
|
* @param string $type |
|
332
|
|
|
* @param string $id |
|
333
|
|
|
* @param string $localKey |
|
334
|
|
|
* @return MorphMany |
|
335
|
|
|
*/ |
|
336
|
2 |
|
public function morphMany($related, $name, $type = null, $id = null, $localKey = null) |
|
337
|
|
|
{ |
|
338
|
|
|
/** |
|
339
|
|
|
* @var Model $instance |
|
340
|
|
|
*/ |
|
341
|
2 |
|
$instance = $this->newRelatedInstance($related); |
|
342
|
|
|
|
|
343
|
2 |
|
list($type, $id) = $this->getMorphs($name, $type, $id); |
|
344
|
|
|
|
|
345
|
2 |
|
$table = $instance->getTable(); |
|
346
|
|
|
|
|
347
|
2 |
|
$localKey = $localKey ?: $this->getKeyName(); |
|
348
|
|
|
|
|
349
|
2 |
|
return $this->newMorphMany($instance->newQuery(), $this, $table.'.'.$type, $table.'.'.$id, $localKey); |
|
|
|
|
|
|
350
|
|
|
} |
|
351
|
|
|
|
|
352
|
|
|
/** |
|
353
|
|
|
* Instantiate a new MorphMany relationship. |
|
354
|
|
|
* |
|
355
|
|
|
* @param Builder $query |
|
356
|
|
|
* @param Model $parent |
|
357
|
|
|
* @param string $type |
|
358
|
|
|
* @param string $id |
|
359
|
|
|
* @param string $localKey |
|
360
|
|
|
* @return MorphMany |
|
361
|
|
|
*/ |
|
362
|
2 |
|
protected function newMorphMany(Builder $query, Model $parent, $type, $id, $localKey) |
|
363
|
|
|
{ |
|
364
|
2 |
|
return new MorphMany($query, $parent, $type, $id, $localKey); |
|
365
|
|
|
} |
|
366
|
|
|
|
|
367
|
|
|
/** |
|
368
|
|
|
* Define a many-to-many relationship. |
|
369
|
|
|
* |
|
370
|
|
|
* @param string $related |
|
371
|
|
|
* @param string $table |
|
372
|
|
|
* @param string $foreignPivotKey |
|
373
|
|
|
* @param string $relatedPivotKey |
|
374
|
|
|
* @param string $parentKey |
|
375
|
|
|
* @param string $relatedKey |
|
376
|
|
|
* @param string $relation |
|
377
|
|
|
* @return BelongsToMany |
|
378
|
|
|
*/ |
|
379
|
3 |
|
public function belongsToMany($related, $table = null, $foreignPivotKey = null, $relatedPivotKey = null, |
|
380
|
|
|
$parentKey = null, $relatedKey = null, $relation = null) |
|
381
|
|
|
{ |
|
382
|
3 |
|
if (is_null($relation)) { |
|
383
|
3 |
|
$relation = $this->guessBelongsToManyRelation(); |
|
|
|
|
|
|
384
|
|
|
} |
|
385
|
|
|
|
|
386
|
|
|
/** |
|
387
|
|
|
* @var Model $instance |
|
388
|
|
|
*/ |
|
389
|
3 |
|
$instance = $this->newRelatedInstance($related); |
|
390
|
|
|
|
|
391
|
3 |
|
$foreignPivotKey = $foreignPivotKey ?: $this->getForeignKey(); |
|
392
|
|
|
|
|
393
|
3 |
|
$relatedPivotKey = $relatedPivotKey ?: $instance->getForeignKey(); |
|
394
|
|
|
|
|
395
|
3 |
|
if (is_null($table)) { |
|
396
|
3 |
|
$table = $this->joiningTable($related, $instance); |
|
|
|
|
|
|
397
|
|
|
} |
|
398
|
|
|
|
|
399
|
3 |
|
return $this->newBelongsToMany( |
|
400
|
3 |
|
$instance->newQuery(), $this, $table, $foreignPivotKey, |
|
|
|
|
|
|
401
|
3 |
|
$relatedPivotKey, $parentKey ?: $this->getKeyName(), |
|
402
|
3 |
|
$relatedKey ?: $instance->getKeyName(), $relation |
|
403
|
|
|
); |
|
404
|
|
|
} |
|
405
|
|
|
|
|
406
|
|
|
/** |
|
407
|
|
|
* Instantiate a new BelongsToMany relationship. |
|
408
|
|
|
* |
|
409
|
|
|
* @param Builder $query |
|
410
|
|
|
* @param Model $parent |
|
411
|
|
|
* @param string $table |
|
412
|
|
|
* @param string $foreignPivotKey |
|
413
|
|
|
* @param string $relatedPivotKey |
|
414
|
|
|
* @param string $parentKey |
|
415
|
|
|
* @param string $relatedKey |
|
416
|
|
|
* @param string $relationName |
|
417
|
|
|
* @return BelongsToMany |
|
418
|
|
|
*/ |
|
419
|
3 |
|
protected function newBelongsToMany(Builder $query, Model $parent, $table, $foreignPivotKey, $relatedPivotKey, |
|
420
|
|
|
$parentKey, $relatedKey, $relationName = null) |
|
421
|
|
|
{ |
|
422
|
3 |
|
return new BelongsToMany($query, $parent, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName); |
|
423
|
|
|
} |
|
424
|
|
|
|
|
425
|
|
|
/** |
|
426
|
|
|
* Define a polymorphic many-to-many relationship. |
|
427
|
|
|
* |
|
428
|
|
|
* @param string $related |
|
429
|
|
|
* @param string $name |
|
430
|
|
|
* @param string $table |
|
431
|
|
|
* @param string $foreignPivotKey |
|
432
|
|
|
* @param string $relatedPivotKey |
|
433
|
|
|
* @param string $parentKey |
|
434
|
|
|
* @param string $relatedKey |
|
435
|
|
|
* @param bool $inverse |
|
436
|
|
|
* @return MorphToMany |
|
437
|
|
|
*/ |
|
438
|
4 |
|
public function morphToMany($related, $name, $table = null, $foreignPivotKey = null, |
|
439
|
|
|
$relatedPivotKey = null, $parentKey = null, |
|
440
|
|
|
$relatedKey = null, $inverse = false) |
|
441
|
|
|
{ |
|
442
|
4 |
|
$caller = $this->guessBelongsToManyRelation(); |
|
443
|
|
|
|
|
444
|
|
|
/** |
|
445
|
|
|
* @var Model $instance |
|
446
|
|
|
*/ |
|
447
|
4 |
|
$instance = $this->newRelatedInstance($related); |
|
448
|
|
|
|
|
449
|
4 |
|
$foreignPivotKey = $foreignPivotKey ?: $name.'_id'; |
|
450
|
|
|
|
|
451
|
4 |
|
$relatedPivotKey = $relatedPivotKey ?: $instance->getForeignKey(); |
|
452
|
|
|
|
|
453
|
4 |
|
$table = $table ?: Str::plural($name); |
|
454
|
|
|
|
|
455
|
4 |
|
return $this->newMorphToMany( |
|
456
|
4 |
|
$instance->newQuery(), $this, $name, $table, |
|
|
|
|
|
|
457
|
4 |
|
$foreignPivotKey, $relatedPivotKey, $parentKey ?: $this->getKeyName(), |
|
458
|
4 |
|
$relatedKey ?: $instance->getKeyName(), $caller, $inverse |
|
459
|
|
|
); |
|
460
|
|
|
} |
|
461
|
|
|
|
|
462
|
|
|
/** |
|
463
|
|
|
* Instantiate a new HasManyThrough relationship. |
|
464
|
|
|
* |
|
465
|
|
|
* @param Builder $query |
|
466
|
|
|
* @param Model $parent |
|
467
|
|
|
* @param string $name |
|
468
|
|
|
* @param string $table |
|
469
|
|
|
* @param string $foreignPivotKey |
|
470
|
|
|
* @param string $relatedPivotKey |
|
471
|
|
|
* @param string $parentKey |
|
472
|
|
|
* @param string $relatedKey |
|
473
|
|
|
* @param string $relationName |
|
474
|
|
|
* @param bool $inverse |
|
475
|
|
|
* @return MorphToMany |
|
476
|
|
|
*/ |
|
477
|
4 |
|
protected function newMorphToMany(Builder $query, Model $parent, $name, $table, $foreignPivotKey, |
|
478
|
|
|
$relatedPivotKey, $parentKey, $relatedKey, |
|
479
|
|
|
$relationName = null, $inverse = false) |
|
480
|
|
|
{ |
|
481
|
4 |
|
return new MorphToMany($query, $parent, $name, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, |
|
482
|
4 |
|
$relationName, $inverse); |
|
483
|
|
|
} |
|
484
|
|
|
|
|
485
|
|
|
/** |
|
486
|
|
|
* Define a polymorphic, inverse many-to-many relationship. |
|
487
|
|
|
* |
|
488
|
|
|
* @param string $related |
|
489
|
|
|
* @param string $name |
|
490
|
|
|
* @param string $table |
|
491
|
|
|
* @param string $foreignPivotKey |
|
492
|
|
|
* @param string $relatedPivotKey |
|
493
|
|
|
* @param string $parentKey |
|
494
|
|
|
* @param string $relatedKey |
|
495
|
|
|
* @return MorphToMany |
|
496
|
|
|
*/ |
|
497
|
2 |
|
public function morphedByMany($related, $name, $table = null, $foreignPivotKey = null, |
|
498
|
|
|
$relatedPivotKey = null, $parentKey = null, $relatedKey = null) |
|
499
|
|
|
{ |
|
500
|
2 |
|
$foreignPivotKey = $foreignPivotKey ?: $this->getForeignKey(); |
|
501
|
|
|
|
|
502
|
2 |
|
$relatedPivotKey = $relatedPivotKey ?: $name.'_id'; |
|
503
|
|
|
|
|
504
|
2 |
|
return $this->morphToMany( |
|
505
|
2 |
|
$related, $name, $table, $foreignPivotKey, |
|
506
|
2 |
|
$relatedPivotKey, $parentKey, $relatedKey, true |
|
507
|
|
|
); |
|
508
|
|
|
} |
|
509
|
|
|
} |
|
510
|
|
|
|