Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document 'morphWithCount' and 'loadMorphCount' methods #6047

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions eloquent-relationships.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
- [Querying Relationship Absence](#querying-relationship-absence)
- [Querying Polymorphic Relationships](#querying-polymorphic-relationships)
- [Counting Related Models](#counting-related-models)
- [Counting Related Models On Polymorphic Relationships](#counting-related-models-on-polymorphic-relationships)
- [Eager Loading](#eager-loading)
- [Constraining Eager Loads](#constraining-eager-loads)
- [Lazy Eager Loading](#lazy-eager-loading)
Expand Down Expand Up @@ -1075,6 +1076,34 @@ If you need to set additional query constraints on the eager loading query, you
$query->where('rating', 5);
}])

<a name="counting-related-models-on-polymorphic-relationships"></a>
### Counting Related Models On Polymorphic Relationships

If you would like to eager load a `morphTo` relationship, as well as nested relationship counts on the various entities that may be returned by that relationship, you may use the `with` method in combination with the `morphTo` relationship's `morphWithCount` method.

In this example, let's assume `Photo` and `Post` models may create `ActivityFeed` models. Additionally, let's assume that `Photo` models are associated with `Tag` models, and `Post` models are associated with `Comment` models.

Using these model definitions and relationships, we may retrieve `ActivityFeed` model instances and eager load all `parentable` models and their respective nested relationship counts:

use Illuminate\Database\Eloquent\Relations\MorphTo;

$activities = ActivityFeed::query()
->with(['parentable' => function (MorphTo $morphTo) {
$morphTo->morphWithCount([
Photo::class => ['tags'],
Post::class => ['comments'],
]);
}])->get();

In addition, you may use the `loadMorphCount` method to eager load all nested relationship counts on the various entities of the polymorphic relation if the `ActivityFeed` models have already been retrieved:

$activities = ActivityFeed::with('parentable')
->get()
->loadMorphCount('parentable', [
Photo::class => ['tags'],
Post::class => ['comments'],
]);

<a name="eager-loading"></a>
## Eager Loading

Expand Down