2
0

WordIndexController.php 2.2 KB

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