2017-07-31 43 views
3

我試圖顯示最受門票排列的受讓人姓名assigned_toLaravel 5.4:如何通過計數(列)的口才?

$accesses = Access::where('state','=','Assigned'); 

$all = Report::where('state', '=', 'Assigned') 
       ->union($accesses) 
       ->orderBy('count(assigned_to)') //THIS IS WRONG 
       ->get(); 
+0

您的排序依據內部使用'DB ::原始()'的laravel計數。 – aldrin27

+0

請您澄清您的答案? –

+0

例如:' - > orderBy(DB :: raw('query here'))' – aldrin27

回答

0

試試這個: -

$all = Report::where('state', '=', 'Assigned') 
      ->select(DB::raw('count(reports.assigned_to) as assigned_to')) 
      ->union($accesses) 
      ->orderBy('assignedto','DESC') 
      ->get(); 
+0

但它只會計數報告表的assigned_to,我希望能夠統計聯合表格的assigned_to。 –

2

你必須使用DB::raw得到它

$all = Report::where('state', '=', 'Assigned') 
      ->select(DB::raw('count(reports.assigned_to) as assigned_to')) 
      ->union($accesses) 
      ->orderBy('assigned_to','DESC') 
      ->get(); 
+2

或者您可以使用'selectRaw' – fubar

+0

,但它只會計數報告表的assigned_to,我希望能夠統計union_d表的assigned_to。 –

+0

所以傳遞查詢,因爲你傳遞給分配給,你在這裏做同樣的方式 – Exprator

0

我想你可以試試這個:

$all = Report::where('state', '=', 'Assigned') 
      ->select(DB::raw('count(reports.assigned_to) as assigned_to'),DB::raw('count(access.assigned_to) as assigned_to_access')) 
      ->leftjoin('access','report.access.id','=','access.id') 
      ->union($accesses) 
      ->orderBy('assigned_to','DESC') 
      ->orderBy('assigned_to_access','DESC') 
      ->get(); 

希望這有助於爲你 !!!

1

嘗試這樣

$accesses = Access::where('state','=','Assigned'); 

$all = Report::where('state', '=', 'Assigned') 
       ->union($accesses) 
       ->orderBy(DB::raw('count(assigned_to)'),'DESC') 
       ->get(); 
+0

@Grant你試過嗎? –