1414use PDPhilip \Elasticsearch \Relations \MorphTo ;
1515use PDPhilip \Elasticsearch \Relations \MorphOne ;
1616use PDPhilip \Elasticsearch \Eloquent \Model as ParentModel ;
17+ use Illuminate \Database \Eloquent \Builder as EloquentBuilder ;
1718
1819
1920trait HybridRelations
@@ -24,158 +25,158 @@ trait HybridRelations
2425 public function hasOne ($ related , $ foreignKey = null , $ localKey = null )
2526 {
2627 $ foreignKey = $ foreignKey ? : $ this ->getForeignKey ();
27-
28+
2829 $ instance = new $ related ;
29-
30+
3031 $ localKey = $ localKey ? : $ this ->getKeyName ();
31-
32+
3233 return new HasOne ($ instance ->newQuery (), $ this , $ foreignKey , $ localKey );
3334 }
34-
35+
3536 /**
3637 * @inheritDoc
3738 */
3839 public function morphOne ($ related , $ name , $ type = null , $ id = null , $ localKey = null )
3940 {
4041 $ instance = new $ related ;
41-
42+
4243 [$ type , $ id ] = $ this ->getMorphs ($ name , $ type , $ id );
43-
44+
4445 $ localKey = $ localKey ? : $ this ->getKeyName ();
45-
46+
4647 return new MorphOne ($ instance ->newQuery (), $ this , $ type , $ id , $ localKey );
47-
48+
4849 }
49-
50+
5051 /**
5152 * @inheritDoc
5253 */
5354 public function hasMany ($ related , $ foreignKey = null , $ localKey = null )
5455 {
5556 $ foreignKey = $ foreignKey ? : $ this ->getForeignKey ();
56-
57+
5758 $ instance = new $ related ;
58-
59+
5960 $ localKey = $ localKey ? : $ this ->getKeyName ();
60-
61+
6162 return new HasMany ($ instance ->newQuery (), $ this , $ foreignKey , $ localKey );
6263 }
63-
64+
6465 /**
6566 * @inheritDoc
6667 */
6768 public function morphMany ($ related , $ name , $ type = null , $ id = null , $ localKey = null )
6869 {
69-
70+
7071 $ instance = new $ related ;
71-
72+
7273 [$ type , $ id ] = $ this ->getMorphs ($ name , $ type , $ id );
73-
74+
7475 $ table = $ instance ->getTable ();
75-
76+
7677 $ localKey = $ localKey ? : $ this ->getKeyName ();
77-
78+
7879 return new MorphMany ($ instance ->newQuery (), $ this , $ type , $ id , $ localKey );
7980 }
80-
81+
8182 /**
8283 * @inheritDoc
8384 */
8485 public function belongsTo ($ related , $ foreignKey = null , $ otherKey = null , $ relation = null )
8586 {
86-
87+
8788 if ($ relation === null ) {
8889 [$ current , $ caller ] = debug_backtrace (false , 2 );
89-
90+
9091 $ relation = $ caller ['function ' ];
9192 }
92-
93-
93+
94+
9495 if ($ foreignKey === null ) {
9596 $ foreignKey = Str::snake ($ relation ).'_id ' ;
9697 }
97-
98+
9899 $ instance = new $ related ;
99-
100+
100101 $ query = $ instance ->newQuery ();
101-
102+
102103 $ otherKey = $ otherKey ? : $ instance ->getKeyName ();
103-
104+
104105 return new BelongsTo ($ query , $ this , $ foreignKey , $ otherKey , $ relation );
105106 }
106-
107+
107108 /**
108109 * @inheritDoc
109110 */
110111 public function morphTo ($ name = null , $ type = null , $ id = null , $ ownerKey = null )
111112 {
112113 if ($ name === null ) {
113114 [$ current , $ caller ] = debug_backtrace (DEBUG_BACKTRACE_IGNORE_ARGS , 2 );
114-
115+
115116 $ name = Str::snake ($ caller ['function ' ]);
116117 }
117-
118+
118119 [$ type , $ id ] = $ this ->getMorphs ($ name , $ type , $ id );
119-
120+
120121 if (($ class = $ this ->$ type ) === null ) {
121122 return new MorphTo (
122123 $ this ->newQuery (), $ this , $ id , $ ownerKey , $ type , $ name
123124 );
124125 }
125-
126+
126127 $ class = $ this ->getActualClassNameForMorph ($ class );
127-
128+
128129 $ instance = new $ class ;
129-
130+
130131 $ ownerKey = $ ownerKey ?? $ instance ->getKeyName ();
131-
132+
132133 return new MorphTo (
133134 $ instance ->newQuery (), $ this , $ id , $ ownerKey , $ type , $ name
134135 );
135136 }
136-
137+
137138 /**
138139 * @inheritDoc
139140 */
140141 public function belongsToMany ($ related , $ collection = null , $ foreignKey = null , $ otherKey = null , $ parentKey = null , $ relatedKey = null , $ relation = null )
141142 {
142-
143+
143144 if ($ relation === null ) {
144145 $ relation = $ this ->guessBelongsToManyRelation ();
145146 }
146-
147-
147+
148+
148149 if (!is_subclass_of ($ related , ParentModel::class)) {
149150 return parent ::belongsToMany ($ related , $ collection , $ foreignKey , $ otherKey , $ parentKey , $ relatedKey , $ relation );
150151 }
151-
152+
152153 $ foreignKey = $ foreignKey ? : $ this ->getForeignKey ().'s ' ;
153154 $ instance = new $ related ;
154155 $ otherKey = $ otherKey ? : $ instance ->getForeignKey ().'s ' ;
155-
156+
156157 if ($ collection === null ) {
157158 $ collection = $ instance ->getTable ();
158159 }
159-
160+
160161 $ query = $ instance ->newQuery ();
161-
162+
162163 return new BelongsToMany ($ query , $ this , $ collection , $ foreignKey , $ otherKey , $ parentKey ? : $ this ->getKeyName (), $ relatedKey ? : $ instance ->getKeyName (), $ relation
163164 );
164165 }
165-
166+
166167 /**
167168 * @inheritDoc
168169 */
169-
170+
170171 protected function guessBelongsToManyRelation ()
171172 {
172173 if (method_exists ($ this , 'getBelongsToManyCaller ' )) {
173174 return $ this ->getBelongsToManyCaller ();
174175 }
175-
176+
176177 return parent ::guessBelongsToManyRelation ();
177178 }
178-
179+
179180 /**
180181 * @inheritdoc
181182 */
@@ -184,7 +185,7 @@ public function newEloquentBuilder($query)
184185 if (is_subclass_of ($ this , ParentModel::class)) {
185186 return new Builder ($ query );
186187 }
187-
188- return new Builder ($ query );
188+
189+ return new EloquentBuilder ($ query );
189190 }
190191}
0 commit comments