@@ -93,21 +93,6 @@ public function buildQuery($query, Request $request)
9393 return $ query ;
9494 }
9595
96- /**
97- * Force table for some operations.
98- *
99- * @param Builder|Relation|SoftDeletes $query
100- * @param Request $request
101- * @param array $includeDescriptors
102- * @return void
103- */
104- protected function usingTable ($ table , $ callback )
105- {
106- $ this ->table = $ table ;
107- $ callback ();
108- unset($ this ->table );
109- }
110-
11196 /**
11297 * Apply scopes to the given query builder based on the query parameters.
11398 *
@@ -348,14 +333,14 @@ public function getQualifiedFieldName(string $field): string
348333 }
349334
350335 /**
351- * Get the table from a relation.
336+ * Get the model class from a given relation.
352337 *
353338 * @param string $relation
354339 * @return string
355340 */
356- public function getTableNameFromRelation (string $ relation ): string
341+ public function getRelationModelClass (string $ relation ): string
357342 {
358- return ( new $ this ->resourceModelClass )->$ relation ()->getModel ()-> getTable ( );
343+ return get_class (( new $ this ->resourceModelClass )->$ relation ()->getModel ());
359344 }
360345
361346 /**
@@ -555,18 +540,16 @@ public function applyAggregatesToQuery($query, Request $request, array $aggregat
555540 $ aggregateDescriptor ,
556541 $ request
557542 ) {
558- $ this ->usingTable (
559- $ this ->getTableNameFromRelation ($ aggregateDescriptor ['relation ' ]),
560- function () use ($ request , $ aggregateQuery , $ aggregateDescriptor ) {
561- $ this ->applyFiltersToQuery (
562- $ aggregateQuery ,
563- $ request ,
564- $ this ->removeFieldPrefixFromFields (
565- $ aggregateDescriptor ['filters ' ] ?? [],
566- $ aggregateDescriptor ['relation ' ].'. '
567- )
568- );
569- }
543+ $ relationModelClass = $ this ->getRelationModelClass ($ aggregateDescriptor ['relation ' ]);
544+ $ relationQueryBuilder = $ this ->clone ($ relationModelClass );
545+
546+ $ relationQueryBuilder ->applyFiltersToQuery (
547+ $ aggregateQuery ,
548+ $ request ,
549+ $ this ->removeFieldPrefixFromFields (
550+ $ aggregateDescriptor ['filters ' ] ?? [],
551+ $ aggregateDescriptor ['relation ' ].'. '
552+ )
570553 );
571554 },
572555 ], $ aggregateDescriptor ['field ' ] ?? '* ' , $ aggregateDescriptor ['type ' ]);
@@ -599,24 +582,29 @@ public function applyIncludesToQuery($query, Request $request, array $includeDes
599582 foreach ($ includeDescriptors as $ includeDescriptor ) {
600583 $ query ->with ([
601584 $ includeDescriptor ['relation ' ] => function (Relation $ includeQuery ) use ($ includeDescriptor , $ request ) {
602- $ this ->usingTable (
603- $ this ->getTableNameFromRelation ($ includeDescriptor ['relation ' ]),
604- function () use ($ request , $ includeQuery , $ includeDescriptor ) {
605- $ this ->applyFiltersToQuery (
606- $ includeQuery ,
607- $ request ,
608- $ this ->removeFieldPrefixFromFields (
609- $ includeDescriptor ['filters ' ] ?? [],
610- $ includeDescriptor ['relation ' ].'. '
611- )
612- );
613- }
585+ $ relationModelClass = $ this ->getRelationModelClass ($ includeDescriptor ['relation ' ]);
586+ $ relationQueryBuilder = $ this ->clone ($ relationModelClass );
587+
588+ $ relationQueryBuilder ->applyFiltersToQuery (
589+ $ includeQuery ,
590+ $ request ,
591+ $ this ->removeFieldPrefixFromFields (
592+ $ includeDescriptor ['filters ' ] ?? [],
593+ $ includeDescriptor ['relation ' ].'. '
594+ )
614595 );
615596 },
616597 ]);
617598 }
618599 }
619600
601+ public function clone (string $ resourceModelClass ): self
602+ {
603+ return new static (
604+ $ resourceModelClass , $ this ->paramsValidator , $ this ->relationsResolver , $ this ->searchBuilder
605+ );
606+ }
607+
620608 protected function removeFieldPrefixFromFields (array $ array , string $ search )
621609 {
622610 return collect ($ array )
0 commit comments