Obtenga la fila más reciente con group by y Laravel
Aunque hay varias preguntas como esta, no puedo hacer que mi consulta devuelva la fila con la fecha más reciente con un grupo por.
Tengo la siguiente tabla ...
| message_id | from | to | created_at | status
----------------------------------------------
| 1 | 1 | 2 | 2017-04-06 | 1
| 2 | 1 | 2 | 2017-04-07 | 0
| 3 | 3 | 4 | 2017-04-06 | 1
| 4 | 3 | 4 | 2017-04-07 | 0
----------------------------------------------
y estoy tratando de obtener las filas con la fecha más reciente.
| message_id | from | to | created_at | status
----------------------------------------------
| 2 | 1 | 2 | 2017-04-07 | 0
| 4 | 3 | 4 | 2017-04-07 | 0
Actualmente esta consulta devuelve las filas con la última fecha reciente.
$messages = Message::where('to', Auth::id())
->groupBy('from')
->orderBy('created_at', 'DESC')
->paginate(10);