Laravel 5.4:如何按计数(列)进行排序?

时间:2022-10-15 22:57:22

I am trying to display the names of assignee arranged by the most tickets assigned_to.

我正在尝试显示被转让人的名字由大多数的票转让人安排。

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

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

4 个解决方案

#1


2  

you have to use DB::raw to get it

您必须使用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


1  

Try like this

这样的尝试

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

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

#3


0  

Try this:-

试试这个:

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

#4


0  

I think you can try this :

我想你可以试试这个:

$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();

Hope this help for you !!!

希望这对你有帮助!!!

#1


2  

you have to use DB::raw to get it

您必须使用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


1  

Try like this

这样的尝试

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

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

#3


0  

Try this:-

试试这个:

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

#4


0  

I think you can try this :

我想你可以试试这个:

$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();

Hope this help for you !!!

希望这对你有帮助!!!