Thursday, March 2, 2017

Is there a method or a way that can constraining eager loading and nested eager loading at the same time?

We can use Constraining Eager Loads like:

$users = App\User::with(['author' => function ($query) {
    $query->where('isActive', '1');
}])->get();

And we can use Nested Eager Loading like:

$books = App\Book::with('author.books')->get();

But is there a way to use them both at the same time. I mean in the example above eager load with authors that are active and with eager loading books of the author at the same time? Is there a method or a way for this?




via Skeletor

Advertisement