I am creating an inbox system, and I want to get the last message per conversation.
Message Table
id sender_id sent_to_id body created_at updated_at
1 2 1 hello 2019-06-01 20:20:01
2 1 2 ok 2019-06-02 23:20:01
3 3 1 yes 2019-06-01 17:20:01
The result should look like this :
2 1 2 ok 2019-06-02 23:20:01
3 3 1 yes 2019-06-01 17:20:01
Message Model
public function sender()
{
return $this->belongsTo(User::class, 'sender_id');
}
public function receiver()
{
return $this->belongsTo(User::class, 'sent_to_id');
}