0

i have a join query in laravel that i want to group by and orderby together but always just one of them working and groupby is not working with orderby any advice ?? my code is like below :

 $data = $query->join('accommodation_rooms as cr', 'accommodations.id', '=', 'cr.accommodation_id')

            ->join('room_pricing_histories as dr','cr.id', '=', 'dr.accommodation_room_id')
//          ->select('cr.id')
           ->groupBy('dr.sales_price','accommodations.id')
           ->orderBy('dr.sales_price', 'desc')
           ->select('dr.sales_price', 'accommodations.*');


        return $data;
Machavity
  • 30,841
  • 27
  • 92
  • 100
Farshad
  • 1,830
  • 6
  • 38
  • 70
  • 1
    https://stackoverflow.com/questions/31857961/order-by-before-group-by-using-eloquent-laravel – Farrukh Ayyaz Oct 30 '19 at 19:25
  • You are not selecting an aggregate. – Namoshek Oct 30 '19 at 19:51
  • 1
    in the generated MySQL query, `ORDER BY` is evaluated after the `GROUP BY`. The value returned for the non-aggregate expression `sales_price` will be from some row in the collapsed group, but there is no guarantee that this is the highest value in the group. A non-standard MySQL extension allows the query to execute without an error, but if we were to enable more standard behavior by including ONLY_FULL_GROUP_BY in sql_mode, then MySQL would throw an error (like most other relational databases.) – spencer7593 Oct 30 '19 at 19:58

1 Answers1

0

Edit your applications's database config file config/database.php

In mysql array, set strict => false to disable MySQL's strict mode

after select using ->get();