diff --git a/framework/core/src/Api/Controller/AbstractSerializeController.php b/framework/core/src/Api/Controller/AbstractSerializeController.php index ce89c1d23..ee9e907b1 100644 --- a/framework/core/src/Api/Controller/AbstractSerializeController.php +++ b/framework/core/src/Api/Controller/AbstractSerializeController.php @@ -157,7 +157,7 @@ abstract class AbstractSerializeController implements RequestHandlerInterface * * @return string[] */ - protected function getRelationsToLoad(): array + protected function getRelationsToLoad(Collection $models): array { $addedRelations = []; @@ -175,7 +175,7 @@ abstract class AbstractSerializeController implements RequestHandlerInterface * * @return array */ - protected function getRelationCallablesToLoad(): array + protected function getRelationCallablesToLoad(Collection $models): array { $addedRelationCallables = []; @@ -193,8 +193,8 @@ abstract class AbstractSerializeController implements RequestHandlerInterface */ protected function loadRelations(Collection $models, array $relations, ServerRequestInterface $request = null): void { - $addedRelations = $this->getRelationsToLoad(); - $addedRelationCallables = $this->getRelationCallablesToLoad(); + $addedRelations = $this->getRelationsToLoad($models); + $addedRelationCallables = $this->getRelationCallablesToLoad($models); foreach ($addedRelationCallables as $name => $relation) { $addedRelations[] = $name; diff --git a/framework/core/src/Api/Controller/CreateDiscussionController.php b/framework/core/src/Api/Controller/CreateDiscussionController.php index bed612c93..972876f33 100644 --- a/framework/core/src/Api/Controller/CreateDiscussionController.php +++ b/framework/core/src/Api/Controller/CreateDiscussionController.php @@ -14,6 +14,7 @@ use Flarum\Discussion\Command\ReadDiscussion; use Flarum\Discussion\Command\StartDiscussion; use Flarum\Http\RequestUtil; use Illuminate\Contracts\Bus\Dispatcher; +use Illuminate\Database\Eloquent\Collection; use Illuminate\Support\Arr; use Psr\Http\Message\ServerRequestInterface; use Tobscure\JsonApi\Document; @@ -70,6 +71,8 @@ class CreateDiscussionController extends AbstractCreateController ); } + $this->loadRelations(new Collection([$discussion]), $this->extractInclude($request), $request); + return $discussion; } } diff --git a/framework/core/src/Api/Controller/ShowDiscussionController.php b/framework/core/src/Api/Controller/ShowDiscussionController.php index c26e7c2ab..7f870b411 100644 --- a/framework/core/src/Api/Controller/ShowDiscussionController.php +++ b/framework/core/src/Api/Controller/ShowDiscussionController.php @@ -16,6 +16,7 @@ use Flarum\Http\RequestUtil; use Flarum\Http\SlugManager; use Flarum\Post\PostRepository; use Flarum\User\User; +use Illuminate\Database\Eloquent\Collection; use Illuminate\Support\Arr; use Illuminate\Support\Str; use Psr\Http\Message\ServerRequestInterface; @@ -98,9 +99,9 @@ class ShowDiscussionController extends AbstractShowController $this->includePosts($discussion, $request, $postRelationships); } - $discussion->load(array_filter($include, function ($relationship) { + $this->loadRelations(new Collection([$discussion]), array_filter($include, function ($relationship) { return ! Str::startsWith($relationship, 'posts'); - })); + }), $request); return $discussion; } @@ -198,10 +199,29 @@ class ShowDiscussionController extends AbstractShowController return $posts->all(); } - protected function getRelationsToLoad(): array + protected function getRelationsToLoad(Collection $models): array { - $addedRelations = parent::getRelationsToLoad(); + $addedRelations = parent::getRelationsToLoad($models); + + if ($models->first() instanceof Discussion) { + return $addedRelations; + } return $this->getPostRelationships($addedRelations); } + + protected function getRelationCallablesToLoad(Collection $models): array + { + $addedCallableRelations = parent::getRelationCallablesToLoad($models); + + if ($models->first() instanceof Discussion) { + return $addedCallableRelations; + } + + $postCallableRelationships = $this->getPostRelationships(array_keys($addedCallableRelations)); + + return array_intersect_key($addedCallableRelations, array_flip(array_map(function ($relation) { + return "posts.$relation"; + }, $postCallableRelationships))); + } }