VocabularyController.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Vocabulary;
  4. use Illuminate\Http\Request;
  5. use App\Http\Resources\VocabularyResource;
  6. use Illuminate\Support\Facades\Cache;
  7. use Illuminate\Support\Facades\DB;
  8. class VocabularyController extends Controller
  9. {
  10. /**
  11. * Display a listing of the resource.
  12. *
  13. * @return \Illuminate\Http\Response
  14. */
  15. public function index(Request $request)
  16. {
  17. //
  18. switch ($request->get("view")) {
  19. case 'key':
  20. $key = $request->get("key");
  21. $result = Cache::remember("/dict_vocabulary/{$key}",
  22. env('CACHE_EXPIRE',3600*24),
  23. function() use($key){
  24. return Vocabulary::whereRaw('word like ? or word_en like ?',[$key."%",$key."%"])
  25. ->whereOr('word_en','like',$key."%")
  26. ->orderBy('strlen')
  27. ->orderBy('word')
  28. ->take(10)->get();
  29. });
  30. return $this->ok(['rows'=>VocabularyResource::collection($result),'count'=>count($result)]);
  31. break;
  32. }
  33. }
  34. /**
  35. * Store a newly created resource in storage.
  36. *
  37. * @param \Illuminate\Http\Request $request
  38. * @return \Illuminate\Http\Response
  39. */
  40. public function store(Request $request)
  41. {
  42. //
  43. }
  44. /**
  45. * Display the specified resource.
  46. *
  47. * @param \App\Models\Vocabulary $vocabulary
  48. * @return \Illuminate\Http\Response
  49. */
  50. public function show(Vocabulary $vocabulary)
  51. {
  52. //
  53. }
  54. /**
  55. * Update the specified resource in storage.
  56. *
  57. * @param \Illuminate\Http\Request $request
  58. * @param \App\Models\Vocabulary $vocabulary
  59. * @return \Illuminate\Http\Response
  60. */
  61. public function update(Request $request, Vocabulary $vocabulary)
  62. {
  63. //
  64. }
  65. /**
  66. * Remove the specified resource from storage.
  67. *
  68. * @param \App\Models\Vocabulary $vocabulary
  69. * @return \Illuminate\Http\Response
  70. */
  71. public function destroy(Vocabulary $vocabulary)
  72. {
  73. //
  74. }
  75. }