1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Volosyuk\SimpleEloquent\Relations; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Illuminate\Database\Eloquent\Builder; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Include relations definitions to eloquent |
10
|
|
|
* |
11
|
|
|
* @package Volosyuk\SimpleEloquent |
12
|
|
|
*/ |
13
|
|
|
trait HasRelationships |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Instantiate a new HasOne relationship. |
17
|
|
|
* |
18
|
|
|
* @param Builder $query |
19
|
|
|
* @param Model $parent |
20
|
|
|
* @param string $foreignKey |
21
|
|
|
* @param string $localKey |
22
|
|
|
* @return HasOne |
23
|
|
|
*/ |
24
|
2 |
|
protected function newHasOne(Builder $query, Model $parent, $foreignKey, $localKey) |
25
|
|
|
{ |
26
|
2 |
|
return new HasOne($query, $parent, $foreignKey, $localKey); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Instantiate a new MorphOne relationship. |
31
|
|
|
* |
32
|
|
|
* @param Builder $query |
33
|
|
|
* @param Model $parent |
34
|
|
|
* @param string $type |
35
|
|
|
* @param string $id |
36
|
|
|
* @param string $localKey |
37
|
|
|
* @return MorphOne |
38
|
|
|
*/ |
39
|
2 |
|
protected function newMorphOne(Builder $query, Model $parent, $type, $id, $localKey) |
40
|
|
|
{ |
41
|
2 |
|
return new MorphOne($query, $parent, $type, $id, $localKey); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Instantiate a new BelongsTo relationship. |
46
|
|
|
* |
47
|
|
|
* @param Builder $query |
48
|
|
|
* @param Model $child |
49
|
|
|
* @param string $foreignKey |
50
|
|
|
* @param string $ownerKey |
51
|
|
|
* @param string $relation |
52
|
|
|
* @return BelongsTo |
53
|
|
|
*/ |
54
|
2 |
|
protected function newBelongsTo(Builder $query, Model $child, $foreignKey, $ownerKey, $relation) |
55
|
|
|
{ |
56
|
2 |
|
return new BelongsTo($query, $child, $foreignKey, $ownerKey, $relation); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Instantiate a new MorphTo relationship. |
61
|
|
|
* |
62
|
|
|
* @param Builder $query |
63
|
|
|
* @param Model $parent |
64
|
|
|
* @param string $foreignKey |
65
|
|
|
* @param string $ownerKey |
66
|
|
|
* @param string $type |
67
|
|
|
* @param string $relation |
68
|
|
|
* @return MorphTo |
69
|
|
|
*/ |
70
|
2 |
|
protected function newMorphTo(Builder $query, Model $parent, $foreignKey, $ownerKey, $type, $relation) |
71
|
|
|
{ |
72
|
2 |
|
return new MorphTo($query, $parent, $foreignKey, $ownerKey, $type, $relation); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Instantiate a new HasMany relationship. |
77
|
|
|
* |
78
|
|
|
* @param Builder $query |
79
|
|
|
* @param Model $parent |
80
|
|
|
* @param string $foreignKey |
81
|
|
|
* @param string $localKey |
82
|
|
|
* @return HasMany |
83
|
|
|
*/ |
84
|
3 |
|
protected function newHasMany(Builder $query, Model $parent, $foreignKey, $localKey) |
85
|
|
|
{ |
86
|
3 |
|
return new HasMany($query, $parent, $foreignKey, $localKey); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Instantiate a new HasManyThrough relationship. |
91
|
|
|
* |
92
|
|
|
* @param Builder $query |
93
|
|
|
* @param Model $farParent |
94
|
|
|
* @param Model $throughParent |
95
|
|
|
* @param string $firstKey |
96
|
|
|
* @param string $secondKey |
97
|
|
|
* @param string $localKey |
98
|
|
|
* @param string $secondLocalKey |
99
|
|
|
* @return HasManyThrough |
100
|
|
|
*/ |
101
|
2 |
|
protected function newHasManyThrough(Builder $query, Model $farParent, Model $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey) |
102
|
|
|
{ |
103
|
2 |
|
return new HasManyThrough($query, $farParent, $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Instantiate a new HasOneThrough relationship. |
108
|
|
|
* |
109
|
|
|
* @param Builder $query |
110
|
|
|
* @param Model $farParent |
111
|
|
|
* @param Model $throughParent |
112
|
|
|
* @param string $firstKey |
113
|
|
|
* @param string $secondKey |
114
|
|
|
* @param string $localKey |
115
|
|
|
* @param string $secondLocalKey |
116
|
|
|
* @return HasOneThrough |
117
|
|
|
*/ |
118
|
2 |
|
protected function newHasOneThrough(Builder $query, Model $farParent, Model $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey) |
119
|
|
|
{ |
120
|
2 |
|
return new HasOneThrough($query, $farParent, $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Instantiate a new MorphMany relationship. |
125
|
|
|
* |
126
|
|
|
* @param Builder $query |
127
|
|
|
* @param Model $parent |
128
|
|
|
* @param string $type |
129
|
|
|
* @param string $id |
130
|
|
|
* @param string $localKey |
131
|
|
|
* @return MorphMany |
132
|
|
|
*/ |
133
|
2 |
|
protected function newMorphMany(Builder $query, Model $parent, $type, $id, $localKey) |
134
|
|
|
{ |
135
|
2 |
|
return new MorphMany($query, $parent, $type, $id, $localKey); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Instantiate a new BelongsToMany relationship. |
140
|
|
|
* |
141
|
|
|
* @param Builder $query |
142
|
|
|
* @param Model $parent |
143
|
|
|
* @param string $table |
144
|
|
|
* @param string $foreignPivotKey |
145
|
|
|
* @param string $relatedPivotKey |
146
|
|
|
* @param string $parentKey |
147
|
|
|
* @param string $relatedKey |
148
|
|
|
* @param string $relationName |
149
|
|
|
* @return BelongsToMany |
150
|
|
|
*/ |
151
|
3 |
|
protected function newBelongsToMany(Builder $query, Model $parent, $table, $foreignPivotKey, $relatedPivotKey, |
152
|
|
|
$parentKey, $relatedKey, $relationName = null) |
153
|
|
|
{ |
154
|
3 |
|
return new BelongsToMany($query, $parent, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Instantiate a new HasManyThrough relationship. |
159
|
|
|
* |
160
|
|
|
* @param Builder $query |
161
|
|
|
* @param Model $parent |
162
|
|
|
* @param string $name |
163
|
|
|
* @param string $table |
164
|
|
|
* @param string $foreignPivotKey |
165
|
|
|
* @param string $relatedPivotKey |
166
|
|
|
* @param string $parentKey |
167
|
|
|
* @param string $relatedKey |
168
|
|
|
* @param string $relationName |
169
|
|
|
* @param bool $inverse |
170
|
|
|
* @return MorphToMany |
171
|
|
|
*/ |
172
|
4 |
|
protected function newMorphToMany(Builder $query, Model $parent, $name, $table, $foreignPivotKey, |
173
|
|
|
$relatedPivotKey, $parentKey, $relatedKey, |
174
|
|
|
$relationName = null, $inverse = false) |
175
|
|
|
{ |
176
|
4 |
|
return new MorphToMany($query, $parent, $name, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, |
177
|
4 |
|
$relationName, $inverse); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|