0

I want to sort the records using orderBy method in desc order on the created_at field. But my code doesn't take my orderBy method. Don't know what is the problem. Given below is my code.

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; 
}

Can anybody help me?

cathy123
  • 543
  • 2
  • 8
  • 13
  • `orderBy` is not even used in the code!! – Mumen Yassin May 24 '16 at 12:46
  • try ->orderBy('created_at' , 'desc'); – D Coder May 24 '16 at 12:46
  • since i'm using groupBy i could not use orderBy here. so you can find it here, (DB::raw("(select max(created_at) from ordre where ordre.id=hour_logging.ordre_id order by created_at desc) as sub")) – cathy123 May 24 '16 at 12:47
  • I have had a similar issue. I finally used `groupBy` and `orderBy` together, but instead of `->get()`, you need to use `->lists('total_hours', 'sub')`. – LaDude May 24 '16 at 19:14
  • Make sure you address the right `created_at` column of the table you want to sort the column for. The same holds for `customer_id`. – LaDude May 24 '16 at 19:22
  • @Daniela: lists() will be use for dropdownlist in laravel 5 right??? how can we use lists() here??? – cathy123 May 25 '16 at 05:08
  • `lists()` actually creates an array of values that are returned by the sql statement. Did you take a look at the SQL statement that is actually created by the Builder? You can use `->toSql()` (http://stackoverflow.com/questions/18236294/how-do-i-get-the-query-builder-to-output-its-raw-sql-query-as-a-string) on the query and then pass that query to the database directly to see what that statement really returns. – LaDude May 25 '16 at 15:44

1 Answers1

0

try

->orderBy('created_at' , 'desc');
D Coder
  • 572
  • 4
  • 16
  • As i mentioned in the above comment we could not use groupBy and orderBy at a time.. see this line (DB::raw("(select max(created_at) from ordre where ordre.id=hour_logging.ordre_id order by created_at desc) as sub")) where I've used my orderBy method in an another way – cathy123 May 24 '16 at 12:49
  • OK you want groupBy than orderBy – D Coder May 24 '16 at 13:11