$query = Tasks::leftjoin('get_tasks', 'get_tasks.t_id', '=', 'tasks.id') ->where('tasks.release_tasks_status', '!=', 0)//剔除掉未开始的任务 ->where('tasks.channel_id', $channel_id); // 5:已完成 3:待审核 16进行中 17:失效 if (isset($type)) $query->where(function ($que) use ($type, $u_id) { if ($type == 5 ) { $que->where(['get_tasks.task_status' => 5, 'get_tasks.u_id' => $u_id]); } if ( $type == 3) { $que->where(['get_tasks.task_status' => 3, 'get_tasks.u_id' => $u_id]); } if ($type == 16) { //发布任务的状态 0:未开始 1:进行中 2:已失效' $que->where(['tasks.release_tasks_status' => 1]); } if ($type == 17) { //发布任务的状态 0:未开始 1:进行中 2:已失效' $que->where(['tasks.release_tasks_status' => 2]); } }); $result = $query->select('tasks.id', 'tasks.release_tasks_status', 'tasks.task_title', 'tasks.task_img', 'tasks.expires_type', 'tasks.expires_start_at', 'expires_end_at', 'get_number', 'complete_number', 'get_tasks.task_status', 'get_tasks.gt_id', 'tasks.operation_id','get_tasks.u_id','tasks.status') ->groupBy('id') ->orderBy('tasks.is_demand', 'ASC') ->orderBy('tasks.id', 'DESC') ->orderByRaw("CASE WHEN release_tasks_status = 1 THEN 1 WHEN release_tasks_status = 0 THEN 2 WHEN release_tasks_status = 2 THEN 3 END") ->orderByRaw("FIELD(release_tasks_status, " . implode(", ", [1,0,2]) . ")") ->get();
上面orderByRaw 是关于指定字段 指定数值排序规则,两种 ,大家可以根据自己的场景选择