Get latest message (row) per user in Laravel

后端 未结 9 1122
逝去的感伤
逝去的感伤 2021-02-19 01:37

TL;DR: Need latest message from each sender.

In my Laravel application I have two tables:

Users:

  • id
  • name

Messages:

9条回答
  •  孤街浪徒
    2021-02-19 01:46

    Why not simply accessing the messages, like this -

    // get the authenticated user
    $user = \Auth::user(); 
    
    // find the messages for that user
    return User::with('message')->find($user->id)->messages;
    

提交回复
热议问题