SearchController.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Handlers\LearnkuUrlHandler;
  4. use App\Models\BlogArticle;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\DB;
  7. class SearchController extends Controller
  8. {
  9. public function index(Request $request)
  10. {
  11. // Url 操作工具类
  12. $learnkuUrl = new LearnkuUrlHandler(url()->full());
  13. $search = [];
  14. // 搜索词
  15. $search['q'] = $request->q;
  16. // 排序
  17. $search['order'] = $request->order;
  18. if ($search['order']) {
  19. $search['order'] = $this->splitParams($search['order']);
  20. }
  21. // 基础查询语句
  22. $table = BlogArticle::withOrder($request->order)
  23. ->select('blog_articles.*', 'images.path as avatar_path')
  24. ->leftJoin('images', function ($join){
  25. $join->on('images.user_id', '=', 'blog_articles.user_id')
  26. ->where('images.image_type', '=', 'avatar');
  27. });
  28. // 关键字查询
  29. if (!empty($search['q'])) {
  30. $table->where('title', 'like', ('%' . $search['q'] . '%'));
  31. }
  32. // 排序
  33. if (isset($search['order'])) {
  34. $table->orderBy($search['order']['name'], $search['order']['value']);
  35. }
  36. // 分页
  37. $blog_articles = $table->paginate(20);
  38. // dd($blog_articles);
  39. // 排序当前选中值
  40. switch ($learnkuUrl->show('order')) {
  41. case 'reply_count_desc':
  42. $order_select = '评论最多';
  43. break;
  44. case 'created_at_desc':
  45. $order_select = '最新创建';
  46. break;
  47. case 'updated_at_desc':
  48. $order_select = '评论最多';
  49. break;
  50. default:
  51. $order_select = '相关性';
  52. break;
  53. }
  54. // 额外基础数据
  55. $data = [
  56. 'search' => [
  57. // 总条数
  58. 'article_all_num' => $blog_articles->total(),
  59. // 搜索关键词
  60. 'q' => $search['q'],
  61. // 排序: 相关性
  62. 'order_select'=> $order_select,
  63. 'order'=> [
  64. [
  65. 'name' => '相关性', 'icon'=> 'random',
  66. 'href' => $learnkuUrl->delete(['order'=> '']),
  67. ],
  68. [
  69. 'name' => '评论最多', 'icon'=> 'comment outline',
  70. 'href' => $learnkuUrl->update(['order'=> 'reply_count_desc']),
  71. ],
  72. [
  73. 'name' => '最新创建', 'icon'=> 'time',
  74. 'href' => $learnkuUrl->update(['order'=> 'created_at_desc']),
  75. ],
  76. [
  77. 'name' => '最近活跃', 'icon'=> 'time',
  78. 'href' => $learnkuUrl->update(['order'=> 'updated_at_desc'])
  79. ]
  80. ]
  81. ],
  82. ];
  83. $table = null;
  84. return view('pages.blog_articles.index', compact('blog_articles', 'data'));
  85. }
  86. /**
  87. * 拆分参数
  88. * @param string $str 例如: created_at_desc
  89. * @param string $splitter 默认分隔符
  90. * @return array [
  91. * name => ':name', // created_at
  92. * value => ':value', // desc
  93. * ]
  94. */
  95. protected function splitParams($str, $splitter = '_')
  96. {
  97. $export_len = strrpos($str, '_');
  98. return [
  99. 'name' => substr($str, 0, $export_len),
  100. 'value' => substr($str, $export_len + 1),
  101. ];
  102. }
  103. }