2016-05-24 120 views
0

我想要使用orderBy方法在created_at字段上按desc顺序排序记录。但我的代码并不需要我的orderBy方法。不知道是什么问题。以下是我的代码。orderBy方法在laravel 5

public function getLastControll() 
{ 
    $result = Order::select(DB::Raw("(SELECT sum(hour_logging.normal_hours+hour_logging.normal_50pst+hour_logging.normal_100pst)FROM hour_logging WHERE hour_logging.ordre_id=ordre.id) AS total_hours")) 
    ->join('hour_logging','hour_logging.ordre_id','=','ordre.id','left') 
    ->addSelect('ordre.*', (DB::raw("(select max(created_at) from ordre where ordre.id=hour_logging.ordre_id order by created_at desc) as sub"))) 
    ->where('ordre.status', '>', '003') 
    ->where('ordre.main_order', 1) 
    ->groupBy('customer_id') 
    ->get(); 
    return $result; 
} 

有人能帮助我吗?

+0

'orderBy'甚至没有在代码中使用! –

+0

尝试 - > orderBy('created_at','desc'); –

+0

因为我使用groupBy我无法在这里使用orderBy。所以你可以在这里找到它(DB :: raw(“(从ordre选择max(created_at),其中ordre.id = hour_logging.ordre_id order by created_at desc)as sub)”) – cathy123

回答

0

尝试

->orderBy('created_at' , 'desc'); 
+0

正如我在上面的评论中提到的,我们不能一次使用groupBy和orderBy ..看到这一行(DB :: raw(“(从select ordre选择max(created_at)where ordre.id = hour_logging.ordre_id order by created_at desc )as sub“))在那里我以另一种方式使用了我的orderBy方法 – cathy123

+0

确定你想要groupBy比orderBy –