public function addCommentsAjaxHandler(Request $request) { $comment_group_id = $_POST['select-comment']; if ($comment_group_id == 0) { $addGroup = $request->input('comment_group_name'); $this->validate($request, ['comment_group_name' => 'required|unique:comments_groups'], ['comment_group_name.unique' => 'This comment group has already created. Please choose some other name', 'comment_group_name.required' => 'Please Provide Some Group Name.']); $objComment = new Comment_group(); $input = array('comment_group_id' => '', 'comment_group_name' => $addGroup); $result = $objComment->addNewCommentGroup($input); if ($result) { $comment_group_details = Comment_group::orderBy('comment_group_id', 'desc')->first(); $comment_group_id = $comment_group_details['comment_group_id']; } } $comment = $request->input('comment1'); $this->validate($request, ['comment1' => 'required'], ['comment1.required' => 'Whats in your mind, Please enter something']); //if comment_group already exists in cmnt table (update) $objModelComment = Comment::getInstance(); $whereForComment = array('rawQuery' => 'comment_group_id = ?', 'bindParams' => [$comment_group_id]); $commentDetails = $objModelComment->getCommentWhere($whereForComment); // dd($prevCmnt); if ($commentDetails) { $prevCmnt = rtrim($commentDetails->comments, ']'); $comment = explode(PHP_EOL, $comment); // dd($comment); $cmnt1 = ''; foreach ($comment as $cmnt) { if (trim(preg_replace('/\\s+/', ' ', $cmnt)) != '') { $cmnt1 .= json_encode(trim(preg_replace('/\\s+/', ' ', $cmnt))) . ','; } } $cmnt1 = $prevCmnt . ',' . $cmnt1; $cmnt2 = rtrim($cmnt1, ','); $cmnt2 = $cmnt2 . ']'; $dataForUpdateComment = array('comments' => $cmnt2); $updated = $objModelComment->updateCommentWhere($dataForUpdateComment, $whereForComment); if ($updated) { return Redirect::back()->with(['status' => 'success', 'message' => 'Your comment has been successfully added.']); } else { return Redirect::back()->with(['status' => 'Error', 'message' => 'Some Error Occured. Please try again.']); } } else { $objComment = new Comment(); $comment = explode(PHP_EOL, $comment); $cmnt1 = ''; foreach ($comment as $cmnt) { if (trim(preg_replace('/\\s+/', ' ', $cmnt)) != '') { $cmnt1 .= json_encode(trim(preg_replace('/\\s+/', ' ', $cmnt))) . ','; } } $cmnt2 = rtrim($cmnt1, ','); $cmnt2 = '[' . $cmnt2 . ']'; $input = array('comment_group_id' => $comment_group_id, 'comments' => $cmnt2, 'added_by' => '1', 'comment_status' => '1'); $result = $objComment->addNewComment($input); if ($result) { return Redirect::back()->with(['status' => 'Success', 'msg' => 'Your comment has been successfully added.']); } else { return Redirect::back()->with(['status' => 'Error', 'msg' => 'Some Error Occurred, For checking errors, please again click on the add comments for group']); } } }