@@ -164,7 +164,6 @@ protected function isMorphRelation($relation)
164164 * Check if a relation is a HasManyDeep relationship.
165165 *
166166 * @param Relation $model
167- * @return bool
168167 */
169168 protected function isHasManyDeep ($ model ): bool
170169 {
@@ -177,7 +176,6 @@ protected function isHasManyDeep($model): bool
177176 * This is the foreign key on the final related table that points to the intermediate table.
178177 *
179178 * @param Relation $model
180- * @return string
181179 */
182180 protected function getHasManyDeepForeignKey ($ model ): string
183181 {
@@ -188,14 +186,16 @@ protected function getHasManyDeepForeignKey($model): string
188186 $ property = $ reflection ->getProperty ('foreignKeys ' );
189187 $ property ->setAccessible (true );
190188 $ foreignKeys = $ property ->getValue ($ model );
191-
192- if (is_array ($ foreignKeys ) && !empty ($ foreignKeys )) {
189+
190+ if (is_array ($ foreignKeys ) && ! empty ($ foreignKeys )) {
193191 // Get the last foreign key (for the final join)
194192 $ lastFK = end ($ foreignKeys );
195193 if (is_string ($ lastFK ) && str_contains ($ lastFK , '. ' )) {
196194 $ parts = explode ('. ' , $ lastFK );
195+
197196 return end ($ parts );
198197 }
198+
199199 return $ lastFK ;
200200 }
201201 }
@@ -212,6 +212,7 @@ protected function getHasManyDeepForeignKey($model): string
212212 if (method_exists ($ model , 'getQualifiedForeignKeyName ' )) {
213213 $ qualified = $ model ->getQualifiedForeignKeyName ();
214214 $ parts = explode ('. ' , $ qualified );
215+
215216 return end ($ parts );
216217 }
217218
@@ -231,7 +232,6 @@ protected function getHasManyDeepForeignKey($model): string
231232 * This is the local key on the intermediate table (or parent if no intermediate).
232233 *
233234 * @param Relation $model
234- * @return string
235235 */
236236 protected function getHasManyDeepLocalKey ($ model ): string
237237 {
@@ -242,14 +242,16 @@ protected function getHasManyDeepLocalKey($model): string
242242 $ property = $ reflection ->getProperty ('localKeys ' );
243243 $ property ->setAccessible (true );
244244 $ localKeys = $ property ->getValue ($ model );
245-
246- if (is_array ($ localKeys ) && !empty ($ localKeys )) {
245+
246+ if (is_array ($ localKeys ) && ! empty ($ localKeys )) {
247247 // Get the last local key (for the final join)
248248 $ lastLK = end ($ localKeys );
249249 if (is_string ($ lastLK ) && str_contains ($ lastLK , '. ' )) {
250250 $ parts = explode ('. ' , $ lastLK );
251+
251252 return end ($ parts );
252253 }
254+
253255 return $ lastLK ;
254256 }
255257 }
@@ -266,6 +268,7 @@ protected function getHasManyDeepLocalKey($model): string
266268 if (method_exists ($ model , 'getQualifiedLocalKeyName ' )) {
267269 $ qualified = $ model ->getQualifiedLocalKeyName ();
268270 $ parts = explode ('. ' , $ qualified );
271+
269272 return end ($ parts );
270273 }
271274
@@ -278,10 +281,11 @@ protected function getHasManyDeepLocalKey($model): string
278281 $ property = $ reflection ->getProperty ('through ' );
279282 $ property ->setAccessible (true );
280283 $ through = $ property ->getValue ($ model );
281- if (is_array ($ through ) && !empty ($ through )) {
284+ if (is_array ($ through ) && ! empty ($ through )) {
282285 $ firstThrough = is_string ($ through [0 ]) ? $ through [0 ] : get_class ($ through [0 ]);
283286 if (class_exists ($ firstThrough )) {
284287 $ throughModel = new $ firstThrough ;
288+
285289 return $ throughModel ->getKeyName ();
286290 }
287291 }
@@ -300,7 +304,6 @@ protected function getHasManyDeepLocalKey($model): string
300304 *
301305 * @param Relation $model
302306 * @param string $lastAlias
303- * @return string|null
304307 */
305308 protected function getHasManyDeepIntermediateTable ($ model , $ lastAlias ): ?string
306309 {
@@ -312,12 +315,13 @@ protected function getHasManyDeepIntermediateTable($model, $lastAlias): ?string
312315 $ property = $ reflection ->getProperty ('through ' );
313316 $ property ->setAccessible (true );
314317 $ through = $ property ->getValue ($ model );
315-
316- if (is_array ($ through ) && !empty ($ through )) {
318+
319+ if (is_array ($ through ) && ! empty ($ through )) {
317320 // Get the first intermediate model
318321 $ firstThrough = is_string ($ through [0 ]) ? $ through [0 ] : get_class ($ through [0 ]);
319322 if (class_exists ($ firstThrough )) {
320323 $ throughModel = new $ firstThrough ;
324+
321325 return $ throughModel ->getTable ();
322326 }
323327 }
@@ -333,29 +337,30 @@ protected function getHasManyDeepIntermediateTable($model, $lastAlias): ?string
333337 * Get the foreign key for joining to the intermediate table.
334338 *
335339 * @param Relation $model
336- * @return string
337340 */
338341 protected function getHasManyDeepIntermediateForeignKey ($ model ): string
339342 {
340343 // The foreign key on the intermediate table that points to the parent
341344 // For User -> Posts -> Comments, this would be posts.user_id
342345 $ parent = $ model ->getParent ();
343346 $ parentKey = $ parent ->getKeyName ();
344-
347+
345348 // Try to get from relationship definition
346349 try {
347350 $ reflection = new \ReflectionClass ($ model );
348351 if ($ reflection ->hasProperty ('foreignKeys ' )) {
349352 $ property = $ reflection ->getProperty ('foreignKeys ' );
350353 $ property ->setAccessible (true );
351354 $ foreignKeys = $ property ->getValue ($ model );
352-
353- if (is_array ($ foreignKeys ) && !empty ($ foreignKeys )) {
355+
356+ if (is_array ($ foreignKeys ) && ! empty ($ foreignKeys )) {
354357 $ firstFK = $ foreignKeys [0 ];
355358 if (is_string ($ firstFK ) && str_contains ($ firstFK , '. ' )) {
356359 $ parts = explode ('. ' , $ firstFK );
360+
357361 return end ($ parts );
358362 }
363+
359364 return $ firstFK ;
360365 }
361366 }
@@ -371,7 +376,6 @@ protected function getHasManyDeepIntermediateForeignKey($model): string
371376 * Get the local key for joining from the parent to the intermediate table.
372377 *
373378 * @param Relation $model
374- * @return string
375379 */
376380 protected function getHasManyDeepIntermediateLocalKey ($ model ): string
377381 {
@@ -492,16 +496,16 @@ protected function joinEagerLoadedColumn($relation, $relationColumn)
492496 // HasManyDeep relationships can traverse multiple intermediate models
493497 // We need to join through all intermediate models to reach the final related table
494498 $ related = $ model ->getRelated ();
495-
499+
496500 // Get the qualified parent key to determine the first intermediate model
497501 $ qualifiedParentKey = $ model ->getQualifiedParentKeyName ();
498502 $ parentTable = explode ('. ' , $ qualifiedParentKey )[0 ];
499-
503+
500504 // For HasManyDeep, we need to join through intermediate models
501505 // The relationship query already knows the structure, so we'll use it
502506 // First, join to the first intermediate model (if not already joined)
503507 $ intermediateTable = $ this ->getHasManyDeepIntermediateTable ($ model , $ lastAlias );
504-
508+
505509 if ($ intermediateTable && $ intermediateTable !== $ lastAlias ) {
506510 // Join to intermediate table first
507511 if ($ this ->enableEagerJoinAliases ) {
@@ -511,27 +515,27 @@ protected function joinEagerLoadedColumn($relation, $relationColumn)
511515 $ intermediateAlias = $ intermediateTable ;
512516 $ intermediate = $ intermediateTable ;
513517 }
514-
518+
515519 $ intermediateFK = $ this ->getHasManyDeepIntermediateForeignKey ($ model );
516520 $ intermediateLocal = $ this ->getHasManyDeepIntermediateLocalKey ($ model );
517521 $ this ->performJoin ($ intermediate , $ intermediateAlias .'. ' .$ intermediateFK , ltrim ($ lastAlias .'. ' .$ intermediateLocal , '. ' ));
518522 $ lastAlias = $ intermediateAlias ;
519523 }
520-
524+
521525 // Now join to the final related table
522526 if ($ this ->enableEagerJoinAliases ) {
523527 $ table = $ related ->getTable ().' as ' .$ tableAlias ;
524528 } else {
525529 $ table = $ tableAlias = $ related ->getTable ();
526530 }
527-
531+
528532 // Get the foreign key on the related table (points to intermediate)
529533 $ foreignKey = $ this ->getHasManyDeepForeignKey ($ model );
530534 $ localKey = $ this ->getHasManyDeepLocalKey ($ model );
531-
535+
532536 $ foreign = $ tableAlias .'. ' .$ foreignKey ;
533537 $ other = ltrim ($ lastAlias .'. ' .$ localKey , '. ' );
534-
538+
535539 $ lastQuery ->addSelect ($ tableAlias .'. ' .$ relationColumn );
536540 break ;
537541
0 commit comments