QuestionController.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\QuestionModel;
  5. use Illuminate\Http\Request;
  6. class QuestionController extends Controller
  7. {
  8. /**
  9. * 创建问题
  10. * @param Request $request
  11. * @return array
  12. */
  13. public function create(Request $request)
  14. {
  15. $this->validate($request, [
  16. 'title' => 'required',
  17. 'content' => 'required',
  18. 'type' => 'required'
  19. ]);
  20. $question = new QuestionModel();
  21. $question->fill($request->all());
  22. $question->save();
  23. return response([
  24. 'code' => 200,
  25. 'message' => 'success'
  26. ]);
  27. }
  28. /**
  29. * 更新问题
  30. * @param Request $request
  31. * @param int $question_id
  32. * @return array
  33. */
  34. public function update(Request $request, int $question_id)
  35. {
  36. $question = QuestionModel::findOrFail($question_id);
  37. $question->fill($request->all());
  38. $question->save();
  39. return response([
  40. 'code' => 200,
  41. 'message' => 'success'
  42. ]);
  43. }
  44. /**
  45. * 删除问题
  46. * @param int $question_id
  47. * @return array
  48. * @throws \Exception
  49. */
  50. public function delete(int $question_id)
  51. {
  52. $question = QuestionModel::findOrFail($question_id);
  53. $question->delete();
  54. return response([
  55. 'code' => 200,
  56. 'message' => 'success'
  57. ]);
  58. }
  59. /**
  60. * 获取问题
  61. * @param int $question_id
  62. * @return array
  63. * @throws \Exception
  64. */
  65. public function show(int $question_id)
  66. {
  67. $question = QuestionModel::find($question_id);
  68. return response([
  69. 'code' => 200,
  70. 'message' => 'success',
  71. 'data' => $question
  72. ]);
  73. }
  74. /**
  75. * 问题列表
  76. * @return array
  77. * @throws \Exception
  78. */
  79. public function index(Request $request)
  80. {
  81. $questions = QuestionModel::get();
  82. return response([
  83. 'code' => 200,
  84. 'message' => 'success',
  85. 'data' => $questions
  86. ]);
  87. }
  88. }