I need a return in collection to paginate but i can only a return in array because the query use a natural JOIN. How could I do this query using Laravel collections :
SELECT mensagens.* FROM mensagens NATURAL JOIN ( SELECT id_emissor, MAX(created_at) created_at FROM mensagens WHERE id_receptor = ".$user_id." GROUP BY id_emissor) t
I have used this code:
$mensagem = \DB::select( DB::raw("SELECT mensagens.* FROM mensagens NATURAL JOIN ( SELECT id_emissor, MAX(created_at) created_at FROM mensagens WHERE id_receptor = ".$user_id." GROUP BY id_emissor) t ") );
If you want to use your query as it is then use like this -
Else you can use eloquent also.