Link:  https://reinink.ca/articles/dynamic-relationships-in-laravel-using-subqueries

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

laravel dynamic relationship