/** * Delete comment by id * * @param $commentId */ public function delete($commentId) { $comment = $this->comment->findById($commentId); $sub = !$comment->parent_id ? $comment->comments + 1 : 1; $this->post->update($comment->post, ['comments' => $comment->post->comments - $sub]); if ($comment->parent) { $this->comment->update($comment->parent, ['comments' => $comment->parent->comments - 1]); } $this->comment->delete($comment); }
/** * More comments for parent comment * * @param $comment * @param Request $request * @return null|\Smile\Models\Comment */ public function loadMore($comment, Request $request) { $last = (int) $request->get('last', 0); $comments = $this->comment->findByParentId($comment, $this->currentUser, $last, 5); $partial = $this->view('partials.more-comments', compact('comments'))->render(); return ['partial' => $partial, 'total' => count($comments), 'last' => $comments[$comments->count() - 1]->id, 'hasMore' => $comments->count() != $comments->total()]; }
/** * Create a new comment * * @param User $user * @param Post $post * @param array $data * @return \Smile\Models\Comment */ public function comment(User $user, Post $post, array $data) { $data['user_id'] = $user->id; $data['post_id'] = $post->id; $this->post->update($post, ['comments' => $post->comments + 1]); // Update the parent of the comment with the right number of comments if (isset($data['parent_id'])) { $parent = $this->comment->findById($data['parent_id']); $this->comment->update($parent, ['comments' => $parent->comments + 1]); } $comment = $this->comment->create($data); if ($comment) { event(new CommentWasCreated($user, $post, $comment)); } return $comment; }