2
2
3
3
namespace MongoDB \Laravel \Eloquent ;
4
4
5
+ use Illuminate \Database \Eloquent \Concerns \HasRelationships ;
5
6
use Illuminate \Database \Eloquent \Relations \MorphOne ;
6
7
use Illuminate \Support \Str ;
8
+ use MongoDB \Laravel \Eloquent \Model as MongoDBModel ;
7
9
use MongoDB \Laravel \Helpers \EloquentBuilder ;
8
10
use MongoDB \Laravel \Relations \BelongsTo ;
9
11
use MongoDB \Laravel \Relations \BelongsToMany ;
@@ -21,15 +23,17 @@ trait HybridRelations
21
23
/**
22
24
* Define a one-to-one relationship.
23
25
*
24
- * @param string $related
25
- * @param string $foreignKey
26
- * @param string $localKey
26
+ * @param class- string $related
27
+ * @param string|null $foreignKey
28
+ * @param string|null $localKey
27
29
* @return \Illuminate\Database\Eloquent\Relations\HasOne
30
+ *
31
+ * @see HasRelationships::hasOne()
28
32
*/
29
33
public function hasOne ($ related , $ foreignKey = null , $ localKey = null )
30
34
{
31
35
// Check if it is a relation with an original model.
32
- if (! is_subclass_of ($ related , \ MongoDB \ Laravel \ Eloquent \Model ::class)) {
36
+ if (! is_subclass_of ($ related , MongoDBModel ::class)) {
33
37
return parent ::hasOne ($ related , $ foreignKey , $ localKey );
34
38
}
35
39
@@ -45,17 +49,19 @@ public function hasOne($related, $foreignKey = null, $localKey = null)
45
49
/**
46
50
* Define a polymorphic one-to-one relationship.
47
51
*
48
- * @param string $related
52
+ * @param class- string $related
49
53
* @param string $name
50
- * @param string $type
51
- * @param string $id
52
- * @param string $localKey
54
+ * @param string|null $type
55
+ * @param string|null $id
56
+ * @param string|null $localKey
53
57
* @return \Illuminate\Database\Eloquent\Relations\MorphOne
58
+ *
59
+ * @see HasRelationships::morphOne()
54
60
*/
55
61
public function morphOne ($ related , $ name , $ type = null , $ id = null , $ localKey = null )
56
62
{
57
63
// Check if it is a relation with an original model.
58
- if (! is_subclass_of ($ related , \ MongoDB \ Laravel \ Eloquent \Model ::class)) {
64
+ if (! is_subclass_of ($ related , MongoDBModel ::class)) {
59
65
return parent ::morphOne ($ related , $ name , $ type , $ id , $ localKey );
60
66
}
61
67
@@ -71,15 +77,17 @@ public function morphOne($related, $name, $type = null, $id = null, $localKey =
71
77
/**
72
78
* Define a one-to-many relationship.
73
79
*
74
- * @param string $related
75
- * @param string $foreignKey
76
- * @param string $localKey
80
+ * @param class- string $related
81
+ * @param string|null $foreignKey
82
+ * @param string|null $localKey
77
83
* @return \Illuminate\Database\Eloquent\Relations\HasMany
84
+ *
85
+ * @see HasRelationships::hasMany()
78
86
*/
79
87
public function hasMany ($ related , $ foreignKey = null , $ localKey = null )
80
88
{
81
89
// Check if it is a relation with an original model.
82
- if (! is_subclass_of ($ related , \ MongoDB \ Laravel \ Eloquent \Model ::class)) {
90
+ if (! is_subclass_of ($ related , MongoDBModel ::class)) {
83
91
return parent ::hasMany ($ related , $ foreignKey , $ localKey );
84
92
}
85
93
@@ -95,17 +103,19 @@ public function hasMany($related, $foreignKey = null, $localKey = null)
95
103
/**
96
104
* Define a polymorphic one-to-many relationship.
97
105
*
98
- * @param string $related
106
+ * @param class- string $related
99
107
* @param string $name
100
- * @param string $type
101
- * @param string $id
102
- * @param string $localKey
108
+ * @param string|null $type
109
+ * @param string|null $id
110
+ * @param string|null $localKey
103
111
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
112
+ *
113
+ * @see HasRelationships::morphMany()
104
114
*/
105
115
public function morphMany ($ related , $ name , $ type = null , $ id = null , $ localKey = null )
106
116
{
107
117
// Check if it is a relation with an original model.
108
- if (! is_subclass_of ($ related , \ MongoDB \ Laravel \ Eloquent \Model ::class)) {
118
+ if (! is_subclass_of ($ related , MongoDBModel ::class)) {
109
119
return parent ::morphMany ($ related , $ name , $ type , $ id , $ localKey );
110
120
}
111
121
@@ -126,13 +136,15 @@ public function morphMany($related, $name, $type = null, $id = null, $localKey =
126
136
/**
127
137
* Define an inverse one-to-one or many relationship.
128
138
*
129
- * @param string $related
130
- * @param string $foreignKey
131
- * @param string $otherKey
132
- * @param string $relation
139
+ * @param class- string $related
140
+ * @param string|null $foreignKey
141
+ * @param string|null $ownerKey
142
+ * @param string|null $relation
133
143
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
144
+ *
145
+ * @see HasRelationships::belongsTo()
134
146
*/
135
- public function belongsTo ($ related , $ foreignKey = null , $ otherKey = null , $ relation = null )
147
+ public function belongsTo ($ related , $ foreignKey = null , $ ownerKey = null , $ relation = null )
136
148
{
137
149
// If no relation name was given, we will use this debug backtrace to extract
138
150
// the calling method's name and use that as the relationship name as most
@@ -142,8 +154,8 @@ public function belongsTo($related, $foreignKey = null, $otherKey = null, $relat
142
154
}
143
155
144
156
// Check if it is a relation with an original model.
145
- if (! is_subclass_of ($ related , \ MongoDB \ Laravel \ Eloquent \Model ::class)) {
146
- return parent ::belongsTo ($ related , $ foreignKey , $ otherKey , $ relation );
157
+ if (! is_subclass_of ($ related , MongoDBModel ::class)) {
158
+ return parent ::belongsTo ($ related , $ foreignKey , $ ownerKey , $ relation );
147
159
}
148
160
149
161
// If no foreign key was supplied, we can use a backtrace to guess the proper
@@ -160,19 +172,21 @@ public function belongsTo($related, $foreignKey = null, $otherKey = null, $relat
160
172
// actually be responsible for retrieving and hydrating every relations.
161
173
$ query = $ instance ->newQuery ();
162
174
163
- $ otherKey = $ otherKey ?: $ instance ->getKeyName ();
175
+ $ ownerKey = $ ownerKey ?: $ instance ->getKeyName ();
164
176
165
- return new BelongsTo ($ query , $ this , $ foreignKey , $ otherKey , $ relation );
177
+ return new BelongsTo ($ query , $ this , $ foreignKey , $ ownerKey , $ relation );
166
178
}
167
179
168
180
/**
169
181
* Define a polymorphic, inverse one-to-one or many relationship.
170
182
*
171
183
* @param string $name
172
- * @param string $type
173
- * @param string $id
174
- * @param string $ownerKey
184
+ * @param string|null $type
185
+ * @param string|null $id
186
+ * @param string|null $ownerKey
175
187
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
188
+ *
189
+ * @see HasRelationships::morphTo()
176
190
*/
177
191
public function morphTo ($ name = null , $ type = null , $ id = null , $ ownerKey = null )
178
192
{
@@ -211,20 +225,22 @@ public function morphTo($name = null, $type = null, $id = null, $ownerKey = null
211
225
/**
212
226
* Define a many-to-many relationship.
213
227
*
214
- * @param string $related
215
- * @param string $collection
216
- * @param string $foreignKey
217
- * @param string $otherKey
218
- * @param string $parentKey
219
- * @param string $relatedKey
220
- * @param string $relation
228
+ * @param class- string $related
229
+ * @param string|null $collection
230
+ * @param string|null $foreignPivotKey
231
+ * @param string|null $relatedPivotKey
232
+ * @param string|null $parentKey
233
+ * @param string|null $relatedKey
234
+ * @param string|null $relation
221
235
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
236
+ *
237
+ * @see HasRelationships::belongsToMany()
222
238
*/
223
239
public function belongsToMany (
224
240
$ related ,
225
241
$ collection = null ,
226
- $ foreignKey = null ,
227
- $ otherKey = null ,
242
+ $ foreignPivotKey = null ,
243
+ $ relatedPivotKey = null ,
228
244
$ parentKey = null ,
229
245
$ relatedKey = null ,
230
246
$ relation = null
@@ -237,12 +253,12 @@ public function belongsToMany(
237
253
}
238
254
239
255
// Check if it is a relation with an original model.
240
- if (! is_subclass_of ($ related , \ MongoDB \ Laravel \ Eloquent \Model ::class)) {
256
+ if (! is_subclass_of ($ related , MongoDBModel ::class)) {
241
257
return parent ::belongsToMany (
242
258
$ related ,
243
259
$ collection ,
244
- $ foreignKey ,
245
- $ otherKey ,
260
+ $ foreignPivotKey ,
261
+ $ relatedPivotKey ,
246
262
$ parentKey ,
247
263
$ relatedKey ,
248
264
$ relation
@@ -252,11 +268,11 @@ public function belongsToMany(
252
268
// First, we'll need to determine the foreign key and "other key" for the
253
269
// relationship. Once we have determined the keys we'll make the query
254
270
// instances as well as the relationship instances we need for this.
255
- $ foreignKey = $ foreignKey ?: $ this ->getForeignKey ().'s ' ;
271
+ $ foreignPivotKey = $ foreignPivotKey ?: $ this ->getForeignKey ().'s ' ;
256
272
257
273
$ instance = new $ related ;
258
274
259
- $ otherKey = $ otherKey ?: $ instance ->getForeignKey ().'s ' ;
275
+ $ relatedPivotKey = $ relatedPivotKey ?: $ instance ->getForeignKey ().'s ' ;
260
276
261
277
// If no table name was provided, we can guess it by concatenating the two
262
278
// models using underscores in alphabetical order. The two model names
@@ -274,8 +290,8 @@ public function belongsToMany(
274
290
$ query ,
275
291
$ this ,
276
292
$ collection ,
277
- $ foreignKey ,
278
- $ otherKey ,
293
+ $ foreignPivotKey ,
294
+ $ relatedPivotKey ,
279
295
$ parentKey ?: $ this ->getKeyName (),
280
296
$ relatedKey ?: $ instance ->getKeyName (),
281
297
$ relation
@@ -301,7 +317,7 @@ protected function guessBelongsToManyRelation()
301
317
*/
302
318
public function newEloquentBuilder ($ query )
303
319
{
304
- if (is_subclass_of ( $ this , \ MongoDB \ Laravel \ Eloquent \Model::class) ) {
320
+ if ($ this instanceof MongoDBModel ) {
305
321
return new Builder ($ query );
306
322
}
307
323
0 commit comments