QuestionController.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace App\Http\Controllers\Admin\Invite;
  3. use App\Http\Resources\Admin\Invite\QuestionCollection;
  4. use App\Models\Invite\QuestionTemplateModel;
  5. use App\Models\User\LikeInviteQuestionModel;
  6. use App\Http\Controllers\Controller;
  7. use Illuminate\Http\Request;
  8. class QuestionController extends Controller
  9. {
  10. /**
  11. * 问题列表
  12. * @param Request $request
  13. * @return QuestionCollection
  14. */
  15. public function index(Request $request)
  16. {
  17. $questions = LikeInviteQuestionModel::when($request->filled('uid'), function ($query) use ($request) {
  18. return $query->where('uid', $request->uid);
  19. })->when($request->get('sex'), function ($query) use ($request) {
  20. return $query->where('sex', $request->sex);
  21. })->when($request->filled('template_id'), function ($query) use ($request) {
  22. return $query->where('template_id', $request->template_id);
  23. })->when($request->get('system'), function ($query) use ($request) {
  24. if ($request->system == 1) {
  25. return $query->where('select', 1);
  26. } else {
  27. return $query->where('select', 0);
  28. }
  29. })->when($request->filled('question_type'), function ($query) use ($request) {
  30. return $query->where('question_type', $request->question_type);
  31. })
  32. ->orderBy($request->get('sort_by', 'thumbs'), 'desc')
  33. ->paginate($request->input('per_page', 20));
  34. foreach ($questions as $question) {
  35. $question->question = QuestionTemplateModel::find($question->template_id);
  36. }
  37. return new QuestionCollection($questions);
  38. }
  39. /**
  40. * 推荐
  41. * @param Request $request
  42. * @param $id
  43. * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  44. */
  45. public function recommend(Request $request, $id)
  46. {
  47. $question = LikeInviteQuestionModel::findOrFail($id);
  48. $question->select = true;
  49. $question->save();
  50. QuestionTemplateModel::where('id', $question->template_id)->increment('select_count');
  51. return response([
  52. 'code' => 200,
  53. 'message' => 'OK',
  54. 'data' => $question
  55. ]);
  56. }
  57. }