Laravel - Relationships - Nested Lazy Eager Loading Morphto
If you would like to eager load a morphTo
relationship, as well as nested relationships on the various entities that may be returned by that relationship, you may use the loadMorph
method.
This method accepts the name of the morphTo
relationship as its first argument, and an array of model / relationship pairs as its second argument. To help illustrate this method, let's consider the following model:
morphTo();
}
}
In this example, let's assume Event
, Photo
, and Post
models may create ActivityFeed
models. Additionally, let's assume that Event
models belong to a Calendar
model, Photo
models are associated with Tag
models, and Post
models belong to an Author
model.
Using these model definitions and relationships, we may retrieve ActivityFeed
model instances and eager load all parentable
models and their respective nested relationships:
$activities = ActivityFeed::with('parentable')
->get()
->loadMorph('parentable', [
Event::class => ['calendar'],
Photo::class => ['tags'],
Post::class => ['author'],
]);