samedi 6 mai 2017

Order by not working with db raw custom query

I have made a following query

$query = 'select cdo.id, cdo.user_id, cdo.created_at, cdo.updated_at, cdo.shipping_charges from certi_dia_orders cdo inner join users u on cdo.user_id = u.id where cdo.type = "Order"';

if(isset($_REQUEST['order-status']) && strtolower($_REQUEST['order-status']) == 'pending'){
        $query .= ' and cdo.status = "under process"';
      } else if(isset($_REQUEST['order-status']) && strtolower($_REQUEST['order-status']) == 'delivered'){
        $query .= ' and cdo.status = "delivered"';
      } else if(isset($_REQUEST['order-status']) && strtolower($_REQUEST['order-status']) == 'all'){

      } else {
        $query .= ' and cdo.status = "under process"';
      }

      if(isset($_REQUEST['buyer_username']) && $_REQUEST['buyer_username']!=''){
        $query .= ' and u.username like "%'.addslashes($_REQUEST['buyer_username']).'%"';
      }

      if(isset($_REQUEST['date_from']) && $_REQUEST['date_from']!='' && isset($_REQUEST['date_to']) && $_REQUEST['date_to']!='' && strtotime($_REQUEST['date_to']) > strtolower($_REQUEST['date_from'])){
        $query .= ' and (cdo.created_at between "'.addslashes($_REQUEST['date_from']).'" and "'.addslashes($_REQUEST['date_to']).'")';
      }

$order = DB::table(DB::raw("(".$query.") as tbl"))->orderBy('id','desc')->paginate(10);

But it does not return records in desending order. I have experienced same problem with my other projects as well.

I mean why orderby is not working as expected? It is showing records in accending order.

What could be problem and possible solutions?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire