/** * Created By Dara on 8/2/2016 * add reply to comment */ public function course(Course $course, $comment_id, Request $request) { $user = $this->user; $this->validate($request, ['content' => 'required']); if ($comment_id) { //check if the comment has been set or not (reply) level 2 comment $comment = Comment::findOrFail($comment_id); $parent_id = $comment->id; $msg = trans('users.answerSent'); $nested = true; } else { //level 1 comment $parent_id = 0; $msg = trans('users.commentSent'); $nested = false; } //add comment to db $newComment = $course->comments()->create(['user_id' => $user->id, 'content' => $request->input('content'), 'parent_id' => $parent_id]); $numComment = $course->comments()->count(); $course->update(['num_comment' => $numComment]); $obj = $course; $model = 'course'; return ['hasCallback' => 1, 'callback' => 'course_comment', 'hasMsg' => 1, 'msgType' => '', 'msg' => $msg, 'returns' => ['newComment' => view('comment.comment', compact('newComment', 'course', 'user', 'obj', 'model'))->render(), 'nested' => $nested, 'numComment' => $numComment]]; }