Flarum\Core\Repository\PostRepository::findByIds PHP Method

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
    public function findByIds(array $ids, User $actor = null)
    {
        $posts = $this->queryIds($ids, $actor)->get();
        $posts = $posts->sort(function ($a, $b) use($ids) {
            $aPos = array_search($a->id, $ids);
            $bPos = array_search($b->id, $ids);
            if ($aPos === $bPos) {
                return 0;
            }
            return $aPos < $bPos ? -1 : 1;
        });
        return $posts;
    }

Usage Example

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);
 }
All Usage Examples Of Flarum\Core\Repository\PostRepository::findByIds