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

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. 微信小程序返回箭头跳转到指定页面

    onUnload: function () { wx.reLaunch({ url: '../me/order-detail', }) },//这里url搞相对路径 wx.reLaunch跳到新页面没 ...

  2. 创建LEANGOO账号

    转自:https://www.leangoo.com/leangoo_guide/leangoo_guide_login.html#toggle-id-2 Leangoo采用SaaS模式运行,通过邮箱 ...

  3. swift学习网址

    一.网站: 0.swift学习者资源分享 1.swift苹果官网:Swift - Overview 2.Swiftist: Home - Swiftist 社区 3.swift中文指南 4.一起swi ...

  4. go语言入门(4)函数

    1,函数的定义格式 函数构成代码执行的逻辑结构.在Go语言中,函数的基本组成为:关键字func.函数名.参数列表.返回值.函数体和返回语句. Go 语言函数定义格式如下 func FuncName(/ ...

  5. 用Python输出一个Fibonacci数列

    斐波那契数列(Fibonacci sequence),又称黄金分割数列.因数学家列昂纳多·斐波那契(Leonardoda Fibonacci)以兔子繁殖为例子而引入,故又称为“兔子数列” 用文字来说, ...

  6. 2019-11-29-解决从旧格式的-csproj-迁移到新格式的-csproj-格式-AssemblyInfo-文件值重复问题...

    title author date CreateTime categories 解决从旧格式的 csproj 迁移到新格式的 csproj 格式 AssemblyInfo 文件值重复问题 lindex ...

  7. php实现雪花算法(ID递增)

    雪花算法简单描述: 最高位是符号位,始终为0,不可用. 41位的时间序列,精确到毫秒级,41位的长度可以使用69年.时间位还有一个很重要的作用是可以根据时间进行排序. 10位的机器标识,10位的长度最 ...

  8. phpMyAdmin出现Fatal error: Maximum execution time of 300 seconds

    在mysql用phpMyAdmin导入大数据的时候出现:Fatal error: Maximum execution time of 300 seconds exceeded in D:/查了很多文章 ...

  9. dedecms织梦系统后台验证码图片不显示的解决方法

    网站迁移后,dedecms织梦系统后台验证码图片不显示的解决方法通用解决方案-取消后台验证码功能因为没有验证码,不能进后台,所以修改php文件源代码:方法一:打开dede/login.php 找到如下 ...

  10. MyBatis中<![CDATA[ ]]>的使用

    原文地址:https://www.cnblogs.com/catgatp/p/6403382.html <![CDATA[]]>和转义字符 被<![CDATA[]]>这个标记所 ...