3

I am trying to select single from every group where date is max.

I am trying to do with this bellow

|NO | effective   | price     |  level_one| level_two
+---+-------------+----------+|+++++++++++|++++++++++++
|1  | 2011-12-01  |  34       |     1     |    2
|2  | 2011-16-01  |  34       |     1     |    2
|3  | 2011-18-01  | 3434      |     1     |    2
|4  | 2011-16-01  | 3554      |     1     |    3

Result should be

|NO | effective   | price     |  level_one| level_two
+---+-------------+----------+|+++++++++++|++++++++++++
|3  | 2011-18-01  | 3434      |     1     |    2
|4  | 2011-16-01  | 3554      |     1     |    3

But result come

|NO | effective   | price     |  level_one| level_two
+---+-------------+----------+|+++++++++++|++++++++++++
|3  | 2011-12-01  | 34        |     1     |    2
|4  | 2011-16-01  | 3554      |     1     |    3

tried with

$price = App\Price::with('others')
    ->orderBy('effective', 'Desc')
    ->groupBy('level_one','level_two')
    ->get();

1 Answers1

0

Problem on your date format. Normally eloquent and mysql use Y-m-d date format.

Use like this:

$price = App\Price::with('others')
    ->orderByRaw("DATE_FORMAT(effective,'%Y-%d-%m') DESC")
    ->groupBy('level_one','level_two')
    ->get();
Omer YILMAZ
  • 1,234
  • 1
  • 7
  • 15