Firdaus Nasir
Firdaus Nasir

Reputation: 72

Laravel eloquent - use groupBy and having in the same query

Can someone change this sql query to Laravel eloquent?

SELECT * FROM `messages` where recipient_id = 1 group by user_id HAVING MAX(created_at)

Thank you so much

Upvotes: 1

Views: 95

Answers (1)

VIKAS KATARIYA
VIKAS KATARIYA

Reputation: 6005

Use this

First of all you need to create model for Message

Message::where('recipient_id',1)->groupBy('user_id')->havingRaw( 'MAX(created_at)')->get();

Upvotes: 1

Related Questions