簡體   English   中英

如何在 eloquent laravel 中的兩個 orderBy 之間使用 if else

[英]How to use if else between two orderBy in eloquent laravel

如何在 eloquent laravel 中的兩個 orderBy 之間使用 if else

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

不太確定你是如何做事的,但你可能會做這樣的事情:

$query = .... /*you're 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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM