diff --git a/src/Ushahidi/Modules/V5/Actions/Post/HandlePostOnlyParameters.php b/src/Ushahidi/Modules/V5/Actions/Post/HandlePostOnlyParameters.php index 1bcb73185..30a981e40 100644 --- a/src/Ushahidi/Modules/V5/Actions/Post/HandlePostOnlyParameters.php +++ b/src/Ushahidi/Modules/V5/Actions/Post/HandlePostOnlyParameters.php @@ -82,6 +82,14 @@ public function addHydrateRelationships(Post $post, array $hydrates) ]; $relations['enabled_languages'] = true; break; + case 'post_media': + $post->post_media = $post->valuesPostMedia; + $post->post_media = $post->post_media->map(function ($media) { + $media = $media->toArray(); + unset($media['post']); // Remove the 'post' property + return $media; + }); + break; } } return $post; @@ -113,6 +121,8 @@ public function hideUnwantedRelationships(Post $post, array $hydrates) $post->makeHidden('valuesPostsMedia'); $post->makeHidden('valuesPostsSet'); $post->makeHidden('valuesPostTag'); + $post->makeHidden('valuesPostMedia'); + // hide source relationships if (!in_array('message', $hydrates)) { diff --git a/src/Ushahidi/Modules/V5/Models/Post/Post.php b/src/Ushahidi/Modules/V5/Models/Post/Post.php index 053661ef1..c92060cab 100644 --- a/src/Ushahidi/Modules/V5/Models/Post/Post.php +++ b/src/Ushahidi/Modules/V5/Models/Post/Post.php @@ -68,6 +68,7 @@ class Post extends BaseModel ]; public const ALLOWED_RELATIONSHIPS = [ + 'post_media' => ['fields' => [], 'relationships' => ["valuesPostMedia"]], 'locks' => ['fields' => [], 'relationships' => ["locks"]], 'categories' => ['fields' => [], 'relationships' => ["categories"]], 'color' => ['fields' => [], 'relationships' => ["survey"]], @@ -756,9 +757,14 @@ public function valuesRelation() public function valuesPostsMedia() { return $this->hasMany('Ushahidi\Modules\V5\Models\PostValues\PostsMedia', 'post_id', 'id') - ->select('posts_media.*'); + ->selectRaw('posts_media.*'); } + public function valuesPostMedia() + { + return $this->hasMany('Ushahidi\Modules\V5\Models\PostValues\PostMedia', 'post_id', 'id') + ->select('post_media.*'); + } public function valuesPostsSet() { return $this->hasMany('Ushahidi\Modules\V5\Models\PostValues\PostsSet', 'post_id', 'id') diff --git a/src/Ushahidi/Modules/V5/Repository/Post/EloquentPostRepository.php b/src/Ushahidi/Modules/V5/Repository/Post/EloquentPostRepository.php index 29036add9..6bba6d2ec 100644 --- a/src/Ushahidi/Modules/V5/Repository/Post/EloquentPostRepository.php +++ b/src/Ushahidi/Modules/V5/Repository/Post/EloquentPostRepository.php @@ -340,9 +340,13 @@ public function paginate( array $with = [] ): LengthAwarePaginator { $fields = $this->addPostsTableNamePrefix($fields); + // add the order field if not found + if (!in_array('posts.'.$paging->getOrderBy(), $fields)) { + $fields[] = 'posts.'.$paging->getOrderBy(); + } $query = Post::take($paging->getLimit()) //->skip($paging->getSkip()) - ->orderBy($paging->getOrderBy(), $paging->getOrder()); + ->orderBy('posts.'.$paging->getOrderBy(), $paging->getOrder()); $query = $this->setSearchCondition($search_fields, $query); $query = $this->setGuestConditions($query);