findByIds() public method

Find posts by their IDs, optionally making sure they are visible to a certain user.
public findByIds ( array $ids, User $actor = null ) : Illuminate\Database\Eloquent\Collection
$ids array
$actor Flarum\Core\User
return Illuminate\Database\Eloquent\Collection
Example #1
0
 /**
  * {@inheritdoc}
  */
 protected function data(ServerRequestInterface $request, Document $document)
 {
     $actor = $request->getAttribute('actor');
     $filter = $this->extractFilter($request);
     $include = $this->extractInclude($request);
     $where = [];
     if ($postIds = array_get($filter, 'id')) {
         $posts = $this->posts->findByIds(explode(',', $postIds), $actor);
     } else {
         if ($discussionId = array_get($filter, 'discussion')) {
             $where['discussion_id'] = $discussionId;
         }
         if ($number = array_get($filter, 'number')) {
             $where['number'] = $number;
         }
         if ($userId = array_get($filter, 'user')) {
             $where['user_id'] = $userId;
         }
         if ($type = array_get($filter, 'type')) {
             $where['type'] = $type;
         }
         $posts = $this->getPosts($request, $where);
     }
     return $posts->load($include);
 }
Example #2
0
 /**
  * {@inheritdoc}
  */
 protected function data(ServerRequestInterface $request, Document $document)
 {
     $actor = $request->getAttribute('actor');
     $filter = $this->extractFilter($request);
     $include = $this->extractInclude($request);
     if ($postIds = array_get($filter, 'id')) {
         $postIds = explode(',', $postIds);
     } else {
         $postIds = $this->getPostIds($request);
     }
     $posts = $this->posts->findByIds($postIds, $actor);
     return $posts->load($include);
 }
Example #3
0
 /**
  * Load relevant posts onto each discussion using information from the
  * search.
  *
  * @param Collection $discussions
  * @param DiscussionSearch $search
  */
 protected function loadRelevantPosts(Collection $discussions, DiscussionSearch $search)
 {
     $postIds = [];
     foreach ($search->getRelevantPostIds() as $relevantPostIds) {
         $postIds = array_merge($postIds, array_slice($relevantPostIds, 0, 2));
     }
     $posts = $postIds ? $this->posts->findByIds($postIds, $search->getActor())->load('user')->all() : [];
     foreach ($discussions as $discussion) {
         $discussion->relevantPosts = array_filter($posts, function ($post) use($discussion) {
             return $post->discussion_id == $discussion->id;
         });
     }
 }