LARAVEL MYSQL how to use natural JOIN

1.1k views Asked by At

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 ") );

1

There are 1 answers

0
Suniti Yadav On

If you want to use your query as it is then use like this -

\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");

Else you can use eloquent also.