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

findOrFail() public method

Find a post by ID, optionally making sure it is visible to a certain user, or throw an exception.
public findOrFail ( integer $id, User $actor = null ) : Post
$id integer
$actor Flarum\Core\User
return Flarum\Core\Post
    public function findOrFail($id, User $actor = null)
    {
        $posts = $this->findByIds([$id], $actor);
        if (!count($posts)) {
            throw new ModelNotFoundException();
        }
        return $posts->first();
    }

Usage Example

Example #1
0
 /**
  * @param EditPost $command
  * @return \Flarum\Core\Post
  * @throws \Flarum\Core\Exception\PermissionDeniedException
  */
 public function handle(EditPost $command)
 {
     $actor = $command->actor;
     $data = $command->data;
     $post = $this->posts->findOrFail($command->postId, $actor);
     if ($post instanceof CommentPost) {
         $attributes = array_get($data, 'attributes', []);
         if (isset($attributes['content'])) {
             $this->assertCan($actor, 'edit', $post);
             $post->revise($attributes['content'], $actor);
         }
         if (isset($attributes['isHidden'])) {
             $this->assertCan($actor, 'edit', $post);
             if ($attributes['isHidden']) {
                 $post->hide($actor);
             } else {
                 $post->restore();
             }
         }
     }
     $this->events->fire(new PostWillBeSaved($post, $actor, $data));
     $this->validator->assertValid($post->getDirty());
     $post->save();
     $this->dispatchEventsFor($post, $actor);
     return $post;
 }
All Usage Examples Of Flarum\Core\Repository\PostRepository::findOrFail