WordIndexController.php 2.2 KB

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