【问题标题】:orderBy method on laravel 5laravel 5 上的 orderBy 方法
【发布时间】: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"))
  • 我也遇到过类似的问题。我终于把groupByorderBy一起用了,但是不是->get(),你需要用->lists('total_hours', 'sub')
  • 确保您在要对其进行排序的表中找到正确的created_at 列。 customer_id 也是如此。

标签: php laravel-5


【解决方案1】:

试试

->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")) 我以另一种方式使用我的 orderBy 方法
  • OK 你想要 groupBy 而不是 orderBy
猜你喜欢
  • 2015-08-06
  • 2015-05-07
  • 2018-08-26
  • 1970-01-01
  • 1970-01-01
  • 2018-09-16
  • 2018-03-29
  • 2016-01-21
  • 2015-12-29
相关资源
最近更新 更多