0

What's the best way to perform a groupwise maximum using Laravel's Eloquent ORM such that it returns instances of models?

Alexander Trauzzi
  • 7,277
  • 13
  • 68
  • 112

1 Answers1

2

Based on this stackoverflow answer, I was able to create the following:

    $query = MyModel
        ::from(DB::raw(
            'my_models NATURAL JOIN (
                SELECT   user_id, MAX(created_at) created_at
                FROM     my_models
                GROUP BY user_id
            ) t'
        ))
    ;
Community
  • 1
  • 1
Alexander Trauzzi
  • 7,277
  • 13
  • 68
  • 112
  • 1
    39 seconds from question to self-answer! Man, that's fast!! – nurdglaw Jun 11 '13 at 11:49
  • I'm putting it to the community to come up with a better solution than my own. Otherwise, this answer serves to help others who faced the same problem as me. Not so bad when you think about it. – Alexander Trauzzi Jun 11 '13 at 13:58