🔥 Laravel Tip: Eager load and filter by a nested relationship

Sep 15, 2022

If you need to filter a model by a nested relationship and also eager load both of the relationships you can create a closure that takes care of the logic and pass it to both `with` and `whereHas`

Edit: Apparently, there is a `withWhereHas` method in core that I miss, in Laravel v9.16.0 and above you can use that instead.
tip-image-0