【发布时间】:2016-05-24 12:41:16
【问题描述】:
我想在 created_at 字段上使用 orderBy 方法按 desc 顺序对记录进行排序。但是我的代码没有采用我的 orderBy 方法。不知道是什么问题。下面是我的代码。
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;
}
谁能帮帮我?
【问题讨论】:
-
orderBy甚至没有在代码中使用!! -
尝试 ->orderBy('created_at' , 'desc');
-
因为我使用 groupBy 我不能在这里使用 orderBy。所以你可以在这里找到它,(DB::raw("(select max(created_at) from ordre where ordre.id=hour_logging.ordre_id order by created_at desc) as sub"))
-
我也遇到过类似的问题。我终于把
groupBy和orderBy一起用了,但是不是->get(),你需要用->lists('total_hours', 'sub')。 -
确保您在要对其进行排序的表中找到正确的
created_at列。customer_id也是如此。