经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » PHP » 查看文章
Laravel find in set排序实例
来源:jb51  时间:2019/10/10 8:36:43  对本文有异议

做项目遇到个需求,需要对结果集中的数据进行指定规则的顺序排列。

例如,用户状态有四种:

=>未激活;1=>正常;2=>禁用;3=>软删除

现在的需求是,我要按照:正常->未激活->禁用->删除;这个顺序来进行排序,同时按照注册时间降序,网上查了很多资料,国内提到这个的很少,在stackOverFlow上找到了答案!

先上解决方案:

  1. public function index($customer_type = null) {
  2. $search = request('search');
  3. $perPage = request('perPage') ? request('perPage') : 10;
  4. $customer_type = $customer_type ? $customer_type : request('customer_type');
  5. // \DB::enableQueryLog();
  6. $data = Customer::select(['id', 'email', 'user_name', 'nick_name', 'status', 'phone', 'create_time'])
  7. ->where('customer_type', '=', $customer_type)
  8. ->where(function ($query) use ($search) {
  9. if ($search) {
  10. $query->where('user_name', 'like binary', '%' . $search . '%')
  11. ->orWhere('nick_name', 'like binary', '%' . $search . '%')
  12. ->orWhere('phone', 'like binary', '%' . $search . '%')
  13. ->orWhere('email', 'like binary', '%' . $search . '%');
  14. }
  15. })
  16. ->orderByRaw("FIELD(status, " . implode(", ", [1, 2, 0, 3, 4]) . ")")
  17. ->orderBy('create_time', 'desc')
  18. ->paginate($perPage);
  19. // $query = \DB::getQueryLog();
  20. // dd($data);
  21. //追加额外参数,例如搜索条件
  22. $appendData = $data->appends(array(
  23. 'search' => $search,
  24. 'perPage' => $perPage,
  25. ));
  26. return view('admin/customer/customerList', compact('data'));
  27. }

打印出来的sql语句如下:

  1. array:2 [▼
  2. => array:3 [▼
  3. query => select count(*) as aggregate from customer where customer_type = ?”
  4. bindings => array:1 [▼
  5. => 1
  6. ]
  7. time => 2.08
  8. ]
  9. => array:3 [▼
  10. query => select id, email, user_name, nick_name, status, phone, create_time from customer where customer_type = ? order by FIELD(status, 1, 2, 0, 3, 4), create_time desc limit 10 offset 0
  11. bindings => array:1 [▼
  12. => 1
  13. ]
  14. time => 1.2
  15. ]
  16. ]

参考了以下链接:

https://stackoverflow.com/questions/42068986/laravel-weird-behavior-orderbyrawfield

https://stackoverflow.com/questions/34244455/how-to-use-not-find-in-set-in-laravel-5-1

https://stackoverflow.com/questions/35594450/find-in-set-in-laravel-example/35594503

find_in_set 复杂应用:

  1. public function get_teacher_list($timeType, $name, $perPage = 10, $personality = 0, $teachingStyle = 0, $ageType = 0)
  2. {
  3. // \DB::enableQueryLog();
  4. $result_data = DB::table('teacher_info as ti')
  5. ->select('ti.*')
  6. ->join('customer', 'customer.id', '=', 'ti.customer_id')
  7. ->where(function ($query) use ($personality) {
  8. if (trim($personality)) {
  9. $query->whereRaw("find_in_set($personality,ti.label_ids)");
  10. }
  11. })
  12. ->where(function ($query) use ($teachingStyle) {
  13. if (trim($teachingStyle)) {
  14. $query->whereRaw("find_in_set($teachingStyle,ti.label_ids)");
  15. }
  16. })
  17. ->where(function ($query) use ($ageType) {
  18. if (trim($ageType)) {
  19. $ageType = explode('-', $ageType);
  20. $query->whereRaw("DATE_FORMAT(FROM_DAYS(TO_DAYS(NOW())-TO_DAYS(birthday)), '%Y')+0 between $ageType[0] and $ageType[1]");
  21. }
  22. })
  23. ->where(function ($query) use ($timeType) {
  24. //1本周,2下周
  25. if ($timeType == 1) {
  26. $query->where('ti.can_appointment_1', 1);
  27. } elseif ($timeType == 2) {
  28. $query->where('ti.can_appointment_2', 1);
  29. } else {
  30. $query->where('ti.can_appointment_1', '>', 0)
  31. ->orWhere('ti.can_appointment_2', '>', 0);
  32. }
  33. })
  34. ->where(function ($query) use ($name) {
  35. if (trim($name)) {
  36. $query->where('ti.chinese_name', 'like', '%' . $name . '%')
  37. ->orWhere('ti.english_name', 'like', '%' . $name . '%');
  38. }
  39. })
  40. ->where('ti.status', 1)
  41. ->orderBy('ti.total_teach_num', 'desc')
  42. ->orderBy('ti.total_star_num', 'desc')
  43. ->orderBy('ti.satisfaction', 'desc')
  44. ->orderBy('ti.comment_num', 'desc')
  45. ->orderBy('ti.english_name', 'asc')
  46. ->paginate($perPage);
  47. // dd($result_data, \DB::getQueryLog());
  48.  
  49. return $result_data;
  50. }

专门拿出来看一下:

  1. $ids = array(1,17,2);
  2.  
  3. $ids_ordered = implode(',', $ids);
  4.  
  5. $items = User::whereIn('id', $ids)
  6. ->orderByRaw(DB::raw("FIELD(id, $ids_ordered)"))
  7. ->get();

以上这篇Laravel find in set排序实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号