As I have been following Eloquent Performance Patterns by Jonathan Reinink, he has explained how to add dynamic relationship using subquery for models with hasmany relationship.