Laravel find in set排序

Laravel find in set排序

做项目遇到个需求,需要对结果集中的数据进行指定规则的顺序排列。
例如,用户状态有四种:

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

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

public function index($customer_type = null) {
    $search = request('search');
    $perPage = request('perPage') ? request('perPage') : 10;
    $customer_type = $customer_type ? $customer_type : request('customer_type');
    // \DB::enableQueryLog();
    $data = Customer::select(['id', 'email', 'user_name', 'nick_name', 'status', 'phone', 'create_time'])
        ->where('customer_type', '=', $customer_type)
        ->where(function ($query) use ($search) {
            if ($search) {
                $query->where('user_name', 'like binary', '%' . $search . '%')
                    ->orWhere('nick_name', 'like binary', '%' . $search . '%')
                    ->orWhere('phone', 'like binary', '%' . $search . '%')
                    ->orWhere('email', 'like binary', '%' . $search . '%');
            }
        })
        ->orderByRaw("FIELD(status, " . implode(", ", [1, 2, 0, 3, 4]) . ")")
        ->orderBy('create_time', 'desc')
        ->paginate($perPage);
    // $query = \DB::getQueryLog();
    // dd($data);
    //追加额外参数,例如搜索条件
    $appendData = $data->appends(array(
        'search' => $search,
        'perPage' => $perPage,
    ));
    return view('admin/customer/customerList', compact('data'));
}

  打印出来的sql语句如下:

Laravel find in set排序

参考了以下链接:
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 复杂应用:

public function get_teacher_list($timeType, $name, $perPage = 10, $personality = 0, $teachingStyle = 0, $ageType = 0)
{
    // \DB::enableQueryLog();
    $result_data = DB::table('teacher_info as ti')
        ->select('ti.*')
        ->join('customer', 'customer.id', '=', 'ti.customer_id')
        ->where(function ($query) use ($personality) {
            if (trim($personality)) {
                $query->whereRaw("find_in_set($personality,ti.label_ids)");
            }
        })
        ->where(function ($query) use ($teachingStyle) {
            if (trim($teachingStyle)) {
                $query->whereRaw("find_in_set($teachingStyle,ti.label_ids)");
            }
        })
        ->where(function ($query) use ($ageType) {
            if (trim($ageType)) {
                $ageType = explode('-', $ageType);
                $query->whereRaw("DATE_FORMAT(FROM_DAYS(TO_DAYS(NOW())-TO_DAYS(birthday)), '%Y')+0 between $ageType[0] and $ageType[1]");
            }
        })
        ->where(function ($query) use ($timeType) {
            //1本周,2下周
            if ($timeType == 1) {
                $query->where('ti.can_appointment_1', 1);
            } elseif ($timeType == 2) {
                $query->where('ti.can_appointment_2', 1);
            } else {
                $query->where('ti.can_appointment_1', '>', 0)
                    ->orWhere('ti.can_appointment_2', '>', 0);
            }
        })
        ->where(function ($query) use ($name) {
            if (trim($name)) {
                $query->where('ti.chinese_name', 'like', '%' . $name . '%')
                    ->orWhere('ti.english_name', 'like', '%' . $name . '%');
            }
        })
        ->where('ti.status', 1)
        ->orderBy('ti.total_teach_num', 'desc')
        ->orderBy('ti.total_star_num', 'desc')
        ->orderBy('ti.satisfaction', 'desc')
        ->orderBy('ti.comment_num', 'desc')
        ->orderBy('ti.english_name', 'asc')
        ->paginate($perPage);
    // dd($result_data, \DB::getQueryLog());

    return $result_data;
}

  专门拿出来看一下:

$ids = array(1,17,2);

$ids_ordered = implode(',', $ids);

$items = User::whereIn('id', $ids)
 ->orderByRaw(DB::raw("FIELD(id, $ids_ordered)"))
 ->get();

  转载:https://blog.csdn.net/zhezhebie/article/details/78357354

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/112320.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Linux学习——磁盘管理、用户管理、权限管理命令

    一:磁盘管理 df 查看磁盘大小 –h 1024 –H 1000 du 统计磁盘上文件的大小 — du -s /目录 — -h 1024显示二:用户管理 1 Linux中用户和用户组的概念 用户:使用操作系统的人,可以有很多的用户,运行多个用户同时操作一个系统 用户组:具有相同系统权限的一

    2022年2月26日
  • pytest的使用_java中方法的调用或使用的地方

    pytest的使用_java中方法的调用或使用的地方Pytest执行用例规则Pytest在命令行中支持多种方式来运行和选择测试用例1.对某个目录下所有的用例pytest2.对模块中进行测试pytesttest_mod.py3.对文件夹进行

    2022年7月29日
  • Flowable深入浅出-1 Flowable简介

    Flowable深入浅出-1 Flowable简介1Flowable简介什么是BPMN什么是FlowableFlowable官网、开源社区Flowable流程示例什么是BPMN先来看下百度百科的定义:由BPMI(TheBusinessProcessManagementInitiative)开发了一套标准叫业务流程建模符号(BPMN-BusinessProcessModelingNotation)。在BPMINotat…

    2022年5月21日
  • 什么是Web 2.0——下一代软件的设计模式和商业模式 (全文翻译—1 博客版序)

    什么是Web 2.0——下一代软件的设计模式和商业模式 (全文翻译—1 博客版序)这篇文章,是经O’Reilly公司的TimO’Reilly先生授权后,进行全文翻译、出版的。包括:杂志节选版和网络全文版。转载:请遵循CC版权,保留原文作者和翻译作者信息,以及文章链接。感谢:互联网周刊的李洋提供了许多建议。几个文章版本链接:英文原文:http://www.oreillynet.com/pub/a/oreilly/tim/news/2005/09/30/

    2022年7月21日
  • 网站开发团队成员(项目团队)

    1.项目带头人(Boss):通常是项目的发起人,为项目规划企业战略目标,对项目的成败负最终责任。2.项目经理:这个不用说了是项目当然需要PM,建议是通过PMP认证的项目经理,主要负责项目各个过程的管理,以及过程优化降低开发风险。 3.系统架构师:架构师不单单是技术架构,还

    2022年4月10日
  • 将图片转成线稿图_图片怎么转成线稿

    将图片转成线稿图_图片怎么转成线稿1、首先找一张图片,在PS中打开,我随便找了一张自己收藏的壁纸如下图:2、打开图片之后, 我们先把图像处理成黑白。点击菜单栏:图像-调整-黑白。快捷键:Alt+Shitf+Ctrl+B

    2022年8月2日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号