主頁 > 知識庫 > Laravel find in set排序?qū)嵗?/div>

Laravel find in set排序?qū)嵗?/h1>

熱門標簽:聊城電話外呼系統(tǒng)公司 辦理重慶400電話 德陽中江如何申請400開頭電話 銅川電話機器人價格 青白江地圖標注 沛縣400電話辦理 AI電話機器人OEM貼牌 智能電話機器人好公司門薩維 江蘇電商外呼系統(tǒng)運營商

做項目遇到個需求,需要對結果集中的數(shù)據(jù)進行指定規(guī)則的順序排列。

例如,用戶狀態(tài)有四種:

=>未激活;1=>正常;2=>禁用;3=>軟刪除

現(xiàn)在的需求是,我要按照:正常->未激活->禁用->刪除;這個順序來進行排序,同時按照注冊時間降序,網(wǎng)上查了很多資料,國內(nèi)提到這個的很少,在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);
  //追加額外參數(shù),例如搜索條件
  $appendData = $data->appends(array(
    'search' => $search,
    'perPage' => $perPage,
  ));
  return view('admin/customer/customerList', compact('data'));
}

打印出來的sql語句如下:

array:2 [▼
 => array:3 [▼
“query” => “select count(*) as aggregate from customer where customer_type = ?”
“bindings” => array:1 [▼
 => “1”
]
“time” => 2.08
]
 => array:3 [▼
“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”
“bindings” => array:1 [▼
 => “1”
]
“time” => 1.2
]
]

參考了以下鏈接:

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();

以上這篇Laravel find in set排序?qū)嵗褪切【幏窒斫o大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • 對laravel in 查詢的使用方法詳解

標簽:迪慶 烏魯木齊 三亞 濟寧 鷹潭 南寧 赤峰 山南

巨人網(wǎng)絡通訊聲明:本文標題《Laravel find in set排序?qū)嵗?,本文關鍵詞  Laravel,find,set,排序,實例,;如發(fā)現(xiàn)本文內(nèi)容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Laravel find in set排序?qū)嵗废嚓P的同類信息!
  • 本頁收集關于Laravel find in set排序?qū)嵗南嚓P信息資訊供網(wǎng)民參考!
  • 推薦文章