CommentController.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace App\Http\Controllers\Ask;
  3. use App\Models\Answer;
  4. use App\Models\Article;
  5. use App\Models\Comment;
  6. use App\Models\Question;
  7. use Illuminate\Http\Request;
  8. use App\Http\Controllers\Controller;
  9. class CommentController extends Controller
  10. {
  11. /*问题创建校验*/
  12. protected $validateRules = [
  13. 'content' => 'required|max:10000',
  14. ];
  15. /**
  16. * Show the form for creating a new resource.
  17. *
  18. * @return \Illuminate\Http\Response
  19. */
  20. public function store(Request $request)
  21. {
  22. $this->validate($request,$this->validateRules);
  23. $source_type = $request->input('source_type');
  24. $source_id = $request->input('source_id');
  25. if($source_type === 'question'){
  26. $source = Question::find($source_id);
  27. $notify_subject = $source->title;
  28. $notify_type = 'comment_question';
  29. $notify_refer_type = 'question';
  30. $notify_refer_id = 0;
  31. }else if($source_type === 'answer'){
  32. $source = Answer::find($source_id);
  33. $notify_subject = $source->content;
  34. $notify_type = 'comment_answer';
  35. $notify_refer_type = 'answer';
  36. $notify_refer_id = $source->question_id;
  37. }else if($source_type === 'article'){
  38. $source = Article::find($source_id);
  39. $notify_subject = $source->title;
  40. $notify_type = 'comment_article';
  41. $notify_refer_type = 'article';
  42. $notify_refer_id = 0;
  43. }
  44. if(!$source){
  45. abort(404);
  46. }
  47. $data = [
  48. 'user_id' => $request->user()->id,
  49. 'content' => $request->input('content'),
  50. 'source_id' => $source_id,
  51. 'source_type' => get_class($source),
  52. 'to_user_id' => $request->input('to_user_id'),
  53. 'status' => 1,
  54. 'supports' => 0
  55. ];
  56. $comment = Comment::create($data);
  57. /*问题、回答、文章评论数+1*/
  58. $comment->source()->increment('comments');
  59. if( $comment->to_user_id > 0 ){
  60. $this->notify($request->user()->id,$comment->to_user_id,'reply_comment',$notify_subject,$source_id,$comment->content,$notify_refer_type,$notify_refer_id);
  61. }else{
  62. $this->notify($request->user()->id,$source->user_id,$notify_type,$notify_subject,$source_id,$comment->content,$notify_refer_type,$notify_refer_id);
  63. }
  64. return view('theme::comment.item')->with('comment',$comment)
  65. ->with('source_type',$source_type)
  66. ->with('source_id',$source_id);
  67. }
  68. /**
  69. * Display the specified resource.
  70. *
  71. * @param int $id
  72. * @return \Illuminate\Http\Response
  73. */
  74. public function show($source_type,$source_id)
  75. {
  76. if($source_type === 'question'){
  77. $source = Question::find($source_id);
  78. }else if($source_type === 'answer'){
  79. $source = Answer::find($source_id);
  80. }else if($source_type === 'article'){
  81. $source = Article::find($source_id);
  82. }
  83. if(!$source){
  84. abort(404);
  85. }
  86. $comments = $source->comments()->orderBy('supports','desc')->orderBy('created_at','asc')->simplePaginate(15);
  87. return view('theme::comment.paginate')->with('comments',$comments)
  88. ->with('source_type',$source_type)
  89. ->with('source_id',$source_id);
  90. }
  91. }