Get latest message (row) per user in Laravel

后端 未结 9 2499
感动是毒
感动是毒 2021-02-19 01:04

TL;DR: Need latest message from each sender.

In my Laravel application I have two tables:

Users:

  • id
  • name

Messages:

9条回答
  •  面向向阳花
    2021-02-19 02:06

    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;
    

提交回复
热议问题