如何使用if else在两个orderBy之间在雄辩的laravel

uyto3xhc  于 2023-02-10  发布在  其他
关注(0)|答案(1)|浏览(102)

如何使用if else在两个orderBy之间在雄辩的laravel

->orderBy('created_at', 'DESC')
->join('hr_applicants', 'hr_applications.hr_applicant_id', '=', 'hr_applicants.id')
->orderBy('hr_applicants.name', 'ASC')
m1m5dgzv

m1m5dgzv1#

不太清楚你是怎么做的,但你可能会这样做:

$query = .... /*your query code here*/
  ->orderBy('created_at', 'DESC')
  ->join('hr_applicants', 'hr_applications.hr_applicant_id', '=', 'hr_applicants.id')
  ->orderBy('hr_applicants.name', 'ASC');

if ($condition1) {
  $query = $query->orderBy('table.column', 'ASC');
} else {
  $query = $query->orderBy('table.column2', 'DESC');
}

$result = $query->get();

相关问题