Support the ongoing development of Laravel.io →
Requests Database Eloquent
Last updated 2 years ago.
0
Solution

This is what i have been able to produce so far:

DB::table('users as u')
    ->select(['u.name','u.photo','m.*'])
    ->join('message as m','u.id','=','m.useridsend')
    ->join(DB::raw('( SELECT useridsend, MAX(created_at) AS created_at FROM messages GROUP BY useridsend ) AS r'),function($join)
    {
         $join->on('m.useridsend','=','r.useridsend')
         ->On('m.creaded_at','=','r.created_at');
    })->where('m.useridreceive','=',2)->orderBy('m.created_at','DESC')->toSql();

//Tinker Output:
 'select `u`.`name`, `u`.`photo`, `m`.* from `users` as `u` inner join `message` as `m` on `u`.`id` = `m`.`useridsend` inner join ( SELECT useridsend, MAX(created_at) AS created_at FROM messages GROUP BY useridsend ) AS r on `m`.`useridsend` = `r`.`useridsend` and `m`.`creaded_at` = `r`.`created_at` where `m`.`useridreceive` = ? order by `m`.`created_at` desc'
Last updated 2 years ago.
0

It's perfect ! I thanks to you.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

maxrob maxrob Joined 5 Jun 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.