VocabularyController.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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}",10,function() use($key){
  22. return Vocabulary::whereRaw('word like ? or word_en like ?',[$key."%",$key."%"])
  23. ->whereOr('word_en','like',$key."%")
  24. ->orderBy('strlen')
  25. ->orderBy('word')
  26. ->take(10)->get();
  27. });
  28. return $this->ok(['rows'=>VocabularyResource::collection($result),'count'=>count($result)]);
  29. break;
  30. }
  31. }
  32. /**
  33. * Store a newly created resource in storage.
  34. *
  35. * @param \Illuminate\Http\Request $request
  36. * @return \Illuminate\Http\Response
  37. */
  38. public function store(Request $request)
  39. {
  40. //
  41. }
  42. /**
  43. * Display the specified resource.
  44. *
  45. * @param \App\Models\Vocabulary $vocabulary
  46. * @return \Illuminate\Http\Response
  47. */
  48. public function show(Vocabulary $vocabulary)
  49. {
  50. //
  51. }
  52. /**
  53. * Update the specified resource in storage.
  54. *
  55. * @param \Illuminate\Http\Request $request
  56. * @param \App\Models\Vocabulary $vocabulary
  57. * @return \Illuminate\Http\Response
  58. */
  59. public function update(Request $request, Vocabulary $vocabulary)
  60. {
  61. //
  62. }
  63. /**
  64. * Remove the specified resource from storage.
  65. *
  66. * @param \App\Models\Vocabulary $vocabulary
  67. * @return \Illuminate\Http\Response
  68. */
  69. public function destroy(Vocabulary $vocabulary)
  70. {
  71. //
  72. }
  73. }