22
33namespace MongoDB \Laravel \Eloquent ;
44
5+ use Illuminate \Database \Eloquent \Concerns \HasRelationships ;
56use Illuminate \Database \Eloquent \Relations \MorphOne ;
67use Illuminate \Support \Str ;
8+ use MongoDB \Laravel \Eloquent \Model as MongoDBModel ;
79use MongoDB \Laravel \Helpers \EloquentBuilder ;
810use MongoDB \Laravel \Relations \BelongsTo ;
911use MongoDB \Laravel \Relations \BelongsToMany ;
@@ -21,15 +23,17 @@ trait HybridRelations
2123 /**
2224 * Define a one-to-one relationship.
2325 *
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
2729 * @return \Illuminate\Database\Eloquent\Relations\HasOne
30+ *
31+ * @see HasRelationships::hasOne()
2832 */
2933 public function hasOne ($ related , $ foreignKey = null , $ localKey = null )
3034 {
3135 // 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)) {
3337 return parent ::hasOne ($ related , $ foreignKey , $ localKey );
3438 }
3539
@@ -45,17 +49,19 @@ public function hasOne($related, $foreignKey = null, $localKey = null)
4549 /**
4650 * Define a polymorphic one-to-one relationship.
4751 *
48- * @param string $related
52+ * @param class- string $related
4953 * @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
5357 * @return \Illuminate\Database\Eloquent\Relations\MorphOne
58+ *
59+ * @see HasRelationships::morphOne()
5460 */
5561 public function morphOne ($ related , $ name , $ type = null , $ id = null , $ localKey = null )
5662 {
5763 // 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)) {
5965 return parent ::morphOne ($ related , $ name , $ type , $ id , $ localKey );
6066 }
6167
@@ -71,15 +77,17 @@ public function morphOne($related, $name, $type = null, $id = null, $localKey =
7177 /**
7278 * Define a one-to-many relationship.
7379 *
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
7783 * @return \Illuminate\Database\Eloquent\Relations\HasMany
84+ *
85+ * @see HasRelationships::hasMany()
7886 */
7987 public function hasMany ($ related , $ foreignKey = null , $ localKey = null )
8088 {
8189 // 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)) {
8391 return parent ::hasMany ($ related , $ foreignKey , $ localKey );
8492 }
8593
@@ -95,17 +103,19 @@ public function hasMany($related, $foreignKey = null, $localKey = null)
95103 /**
96104 * Define a polymorphic one-to-many relationship.
97105 *
98- * @param string $related
106+ * @param class- string $related
99107 * @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
103111 * @return \Illuminate\Database\Eloquent\Relations\MorphMany
112+ *
113+ * @see HasRelationships::morphMany()
104114 */
105115 public function morphMany ($ related , $ name , $ type = null , $ id = null , $ localKey = null )
106116 {
107117 // 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)) {
109119 return parent ::morphMany ($ related , $ name , $ type , $ id , $ localKey );
110120 }
111121
@@ -126,13 +136,15 @@ public function morphMany($related, $name, $type = null, $id = null, $localKey =
126136 /**
127137 * Define an inverse one-to-one or many relationship.
128138 *
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
133143 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
144+ *
145+ * @see HasRelationships::belongsTo()
134146 */
135- public function belongsTo ($ related , $ foreignKey = null , $ otherKey = null , $ relation = null )
147+ public function belongsTo ($ related , $ foreignKey = null , $ ownerKey = null , $ relation = null )
136148 {
137149 // If no relation name was given, we will use this debug backtrace to extract
138150 // 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
142154 }
143155
144156 // 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 );
147159 }
148160
149161 // 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
160172 // actually be responsible for retrieving and hydrating every relations.
161173 $ query = $ instance ->newQuery ();
162174
163- $ otherKey = $ otherKey ?: $ instance ->getKeyName ();
175+ $ ownerKey = $ ownerKey ?: $ instance ->getKeyName ();
164176
165- return new BelongsTo ($ query , $ this , $ foreignKey , $ otherKey , $ relation );
177+ return new BelongsTo ($ query , $ this , $ foreignKey , $ ownerKey , $ relation );
166178 }
167179
168180 /**
169181 * Define a polymorphic, inverse one-to-one or many relationship.
170182 *
171183 * @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
175187 * @return \Illuminate\Database\Eloquent\Relations\MorphTo
188+ *
189+ * @see HasRelationships::morphTo()
176190 */
177191 public function morphTo ($ name = null , $ type = null , $ id = null , $ ownerKey = null )
178192 {
@@ -211,20 +225,22 @@ public function morphTo($name = null, $type = null, $id = null, $ownerKey = null
211225 /**
212226 * Define a many-to-many relationship.
213227 *
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
221235 * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
236+ *
237+ * @see HasRelationships::belongsToMany()
222238 */
223239 public function belongsToMany (
224240 $ related ,
225241 $ collection = null ,
226- $ foreignKey = null ,
227- $ otherKey = null ,
242+ $ foreignPivotKey = null ,
243+ $ relatedPivotKey = null ,
228244 $ parentKey = null ,
229245 $ relatedKey = null ,
230246 $ relation = null
@@ -237,12 +253,12 @@ public function belongsToMany(
237253 }
238254
239255 // 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)) {
241257 return parent ::belongsToMany (
242258 $ related ,
243259 $ collection ,
244- $ foreignKey ,
245- $ otherKey ,
260+ $ foreignPivotKey ,
261+ $ relatedPivotKey ,
246262 $ parentKey ,
247263 $ relatedKey ,
248264 $ relation
@@ -252,11 +268,11 @@ public function belongsToMany(
252268 // First, we'll need to determine the foreign key and "other key" for the
253269 // relationship. Once we have determined the keys we'll make the query
254270 // instances as well as the relationship instances we need for this.
255- $ foreignKey = $ foreignKey ?: $ this ->getForeignKey ().'s ' ;
271+ $ foreignPivotKey = $ foreignPivotKey ?: $ this ->getForeignKey ().'s ' ;
256272
257273 $ instance = new $ related ;
258274
259- $ otherKey = $ otherKey ?: $ instance ->getForeignKey ().'s ' ;
275+ $ relatedPivotKey = $ relatedPivotKey ?: $ instance ->getForeignKey ().'s ' ;
260276
261277 // If no table name was provided, we can guess it by concatenating the two
262278 // models using underscores in alphabetical order. The two model names
@@ -274,8 +290,8 @@ public function belongsToMany(
274290 $ query ,
275291 $ this ,
276292 $ collection ,
277- $ foreignKey ,
278- $ otherKey ,
293+ $ foreignPivotKey ,
294+ $ relatedPivotKey ,
279295 $ parentKey ?: $ this ->getKeyName (),
280296 $ relatedKey ?: $ instance ->getKeyName (),
281297 $ relation
@@ -301,7 +317,7 @@ protected function guessBelongsToManyRelation()
301317 */
302318 public function newEloquentBuilder ($ query )
303319 {
304- if (is_subclass_of ( $ this , \ MongoDB \ Laravel \ Eloquent \Model::class) ) {
320+ if ($ this instanceof MongoDBModel ) {
305321 return new Builder ($ query );
306322 }
307323
0 commit comments