Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ghostpunter's avatar

Eloquent HasManyThrough should return only latest record from final table

Hi guys, So I have a situation where I have 3 tables and i've successfully defined the hasManyThrough relationship for one of the tables. It works as the docs say it should. However I'm looking for a unique situation where the query will only return the latest record in the final table for each intermediate model found. An example below

projects id - integer name - string

environments id - integer project_id - integer name - string

deployments id - integer environment_id - integer commit_hash - string

So I'll like to access deployments for each project but I only want to return the latest deployment for each project instead of all the deployments. I can't seem the find anything similar in the docs nor anywhere on the internet. I'm trying to go for the clean solution which is why i'm still persisting with this relationship route.

Any advice guys?

0 likes
1 reply

Please or to participate in this conversation.