diff --git a/src/Api/Controller/AbstractSerializeController.php b/src/Api/Controller/AbstractSerializeController.php index 06b53ced3..d3d569ba6 100644 --- a/src/Api/Controller/AbstractSerializeController.php +++ b/src/Api/Controller/AbstractSerializeController.php @@ -148,13 +148,9 @@ abstract class AbstractSerializeController implements RequestHandlerInterface abstract protected function createElement($data, SerializerInterface $serializer); /** - * Eager loads the required relationships. - * - * @param Collection $models - * @param array $relations - * @return void + * Returns the relations to load added by extenders. */ - protected function loadRelations(Collection $models, array $relations): void + protected function getRelationsToLoad(): array { $addedRelations = []; @@ -164,6 +160,20 @@ abstract class AbstractSerializeController implements RequestHandlerInterface } } + return $addedRelations; + } + + /** + * Eager loads the required relationships. + * + * @param Collection $models + * @param array $relations + * @return void + */ + protected function loadRelations(Collection $models, array $relations): void + { + $addedRelations = $this->getRelationsToLoad(); + if (! empty($addedRelations)) { usort($addedRelations, function ($a, $b) { return substr_count($a, '.') - substr_count($b, '.'); diff --git a/src/Api/Controller/ShowDiscussionController.php b/src/Api/Controller/ShowDiscussionController.php index d756d071c..c26e7c2ab 100644 --- a/src/Api/Controller/ShowDiscussionController.php +++ b/src/Api/Controller/ShowDiscussionController.php @@ -187,12 +187,21 @@ class ShowDiscussionController extends AbstractShowController $query->orderBy('created_at')->skip($offset)->take($limit)->with($include); - $posts = $query->get()->all(); + $posts = $query->get(); foreach ($posts as $post) { $post->discussion = $discussion; } - return $posts; + $this->loadRelations($posts, $include); + + return $posts->all(); + } + + protected function getRelationsToLoad(): array + { + $addedRelations = parent::getRelationsToLoad(); + + return $this->getPostRelationships($addedRelations); } }