Skip to content
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
8 changes: 3 additions & 5 deletions eloquent-relationships.md
Original file line number Diff line number Diff line change
Expand Up @@ -1747,15 +1747,14 @@ In this example, Eloquent will only eager load posts where the post's `title` co

If you are eager loading a `morphTo` relationship, Eloquent will run multiple queries to fetch each type of related model. You may add additional constraints to each of these queries using the `MorphTo` relation's `constrain` method:

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\MorphTo;

$comments = Comment::with(['commentable' => function (MorphTo $morphTo) {
$morphTo->constrain([
Post::class => function (Builder $query) {
Post::class => function ($query) {
$query->whereNull('hidden_at');
},
Video::class => function (Builder $query) {
Video::class => function ($query) {
$query->where('type', 'educational');
},
]);
Expand All @@ -1769,9 +1768,8 @@ In this example, Eloquent will only eager load posts that have not been hidden a
You may sometimes find yourself needing to check for the existence of a relationship while simultaneously loading the relationship based on the same conditions. For example, you may wish to only retrieve `User` models that have child `Post` models matching a given query condition while also eager loading the matching posts. You may accomplish this using the `withWhereHas` method:

use App\Models\User;
use Illuminate\Database\Eloquent\Builder;

$users = User::withWhereHas('posts', function (Builder $query) {
$users = User::withWhereHas('posts', function ($query) {
$query->where('featured', true);
})->get();

Expand Down