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

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语句如下:

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

Laravel find in set排序的更多相关文章

  1. Laravel 多态关联中利用关联表相关字段进行排序的问题

    1 目标 1.1 在 Laravel 项目的开发中,多态的需求很常见,按多态关联进行排序的需求也是必须的. 1.2 请想像,我们有一个需求,荣誉栏目多态关联一个档案模型,要求在荣誉中按档案的推荐时间进 ...

  2. laravel操作Redis排序/删除/列表/随机/Hash/集合等方法全解

    Song • 3563 次浏览 • 0 个回复 • 2017年10月简介 Redis模块负责与Redis数据库交互,并提供Redis的相关API支持: Redis模块提供redis与redis.con ...

  3. laravel 同数据表字段比较查询和状态不正规排序

    今天写群组推荐接口,要求未满的群 ( 群最大人数字段maxusers, 群人数字段affiliations_count 都在群组表中),官方,热门(普通群0 ,官方1,热门2 ) 排序的群 同表字段比 ...

  4. laravel中跟据某个特定顺序去排序查出来的数据:FIND_IN_SET

    //返回有顺序的客户id $customer_ids = $customer->bespeakTime($uid); $res = Customer::with('customer_indust ...

  5. Laravel自定义排序

    如果数据库的status字段有0,1,2,3几种状态,如果想让status为1,2的状态排在最前面 那么可以这样: $obj = $obj->orderByRaw(DB::raw('FIELD( ...

  6. laravel 先orderBY再groupby,导致分组后的排序不正确

    //联系过我的经纪人 $appletChats=$this->AppletChat->orderBy('created_at','desc')->where([['user_id', ...

  7. 使用 Laravel 框架:成为微信公众平台开发者

    转: http://ninghao.net/blog/1441 作者:王皓发布于:2014-05-30 13:16更新于:2014-05-31 12:05 我们可以使用Laravel 框架为微信公众平 ...

  8. laravel框架总结(七) -- 数据库操作

      1.使用DB门面进行基本操作 一旦你设置好了数据库连接,就可以使用 DB facade 来进行查找.DB facade 提供每个类型的查找方法:select.update.insert.delet ...

  9. 2016 版 Laravel 系列入门教程(一)【最适合中国人的 Laravel 教程】

    本教程示例代码见: https://github.com/johnlui/Learn-Laravel-5 在任何地方卡住,最快的办法就是去看示例代码. 本文基于 Laravel 5.2 版本,无奈 5 ...

随机推荐

  1. 学习--Spring IOC源码精读

    Spring核心IOC的源码分析(转载) 原文地址:https://javadoop.com/post/spring-ioc#toc11 转载地址:https://blog.csdn.net/nuom ...

  2. 《python解释器源码剖析》第10章--python虚拟机中的一般表达式

    10.0 序 上一章中,我们通过PyEval_EvalFrameEx看到了python虚拟机的整体框架,那么这一章我们将深入到PyEval_EvalFrameEx的各个细节当中,深入剖析python的 ...

  3. Java_Eclipse_Android安装

    转自——链接:https://www.cnblogs.com/summary-2017/p/8073225.html

  4. PXC集群的概述及搭建

    目录 PXC集群的概述及搭建 PXC集群的简介 PXC集群主要由两部分组成: PXC的特性和优点: PXC的局限和劣势: PXC原理描述 在Centos部署基于Mysql高可用方案操作过程 新增节点加 ...

  5. 通过SSH解压缩.tar.gz、.gz、.zip文件的方法

    一般在linux下,常用的压缩格式有如下几个: .tar.gz..gz..zip 解压 .tar.gz 文件命令: tar -zxvf xxx.tar.gz 解压 .gz 文件命令: gunzip x ...

  6. lib异步中断

    基于libusbx-1.0.18-rc1,libusbx现已重新merage到libusb.1. 初始化使用libusb_init初始化libusb,如果是单设备通信,ctx参数可以传NULL,表示使 ...

  7. 使用幕布时,在Session过期后,弹出框加载出登陆的HTML的问题

    思路:在登陆页面判断当前加载的Url是否时login/index ,如果不是跳转到登陆页 //设置或获取对象指定的文件名或路径. var Url = window.location.pathname; ...

  8. python3安装模块,摘自网上

    配置好Python3.6和pip3安装EPEL和IUS软件源 yum install epel-release -y yum install https://centos7.iuscommunity. ...

  9. union共同体

    定义: union 共用体名{ 成员列表}: 与结构体不同的是,共用体的所有成员占用同一段内存,修改一个成员会影响其余成员.但是结构体的各个成员会占不同的内存. 结构体占用的内存大于等于所有成员占用的 ...

  10. Web前端开发——HTML文件结构

    在编写html文件时,把文件保存成 .htm 或 .html的后缀. 基本文件结构 <html> <head> <title></title> < ...