Jonathan Reinink wrote a good tutorial on how to create dynamic relationships in Laravel using subqueries.

laravel dynamic relationship