我有以下PHP代码,我想知道它是否可能有条件地链式方法。如果roles为null或为空([]),我无法调用whereIn。这有效,但是有更简洁的方法吗?
if ($params['roles']){
$query1 = QB::table('personnel_role')
->select(QB::raw('IF(personnel.nickname != "",CONCAT(IFNULL(personnel.first_name,"")," ", IFNULL(personnel.last_name, ""), " (",personnel.nickname,")"), CONCAT(IFNULL(personnel.first_name,"")," ", IFNULL(personnel.last_name,""))) AS "name", personnel.id'))
->leftJoin('personnel', 'personnel_role.personnel_id', '=', 'personnel.id')
->whereIn('personnel_role.role_id', $params['roles'])
->groupBy('personnel.id')->limit($params['skip'])
->offset($params['start']);
}else{
$query1 = QB::table('personnel_role')
->select(QB::raw('IF(personnel.nickname != "",CONCAT(IFNULL(personnel.first_name,"")," ", IFNULL(personnel.last_name, ""), " (",personnel.nickname,")"), CONCAT(IFNULL(personnel.first_name,"")," ", IFNULL(personnel.last_name,""))) AS "name", personnel.id'))
->leftJoin('personnel', 'personnel_role.personnel_id', '=', 'personnel.id')
->groupBy('personnel.id')
->limit($params['skip'])
->offset($params['start']);
}
答案 0 :(得分:2)
试试这个:
Intl.DateTimeFormat().resolvedOptions().timeZone
答案 1 :(得分:1)
因为除whereIn
之外,您的查询基本相同,所以您可以在有条件的情况下自行解决问题。
$query1 = QB::table('personnel_role')
->select(QB::raw('IF(personnel.nickname != "",CONCAT(IFNULL(personnel.first_name,"")," ", IFNULL(personnel.last_name, ""), " (",personnel.nickname,")"), CONCAT(IFNULL(personnel.first_name,"")," ", IFNULL(personnel.last_name,""))) AS "name", personnel.id'))
->leftJoin('personnel', 'personnel_role.personnel_id', '=', 'personnel.id')
->groupBy('personnel.id')->limit($params['skip'])
->offset($params['start']);
if($params['role']) {
$query1 = $query1->whereIn('personnel_role.role_id', $params['roles']);
}