-
Notifications
You must be signed in to change notification settings - Fork 11k
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
[5.8] Add support for typed eager loads #28647
Conversation
d4dd81c
to
749515b
Compare
749515b
to
b80a1c8
Compare
e2a461c
to
f661771
Compare
Can you explain the code example? I can't understand what it is doing from just reading it. Also show the alternative syntax to accomplish this in current Laravel 5.8 without this PR. |
@taylorotwell the test should clarify things further: public function test_with_morph_loading()
{
$comments = Comment::query()
->with(['commentable' => function (MorphTo $morphTo) {
$morphTo->withMorph(Post::class, ['user']);
}])
->get();
$this->assertTrue($comments[0]->relationLoaded('commentable'));
$this->assertTrue($comments[0]->commentable->relationLoaded('user'));
$this->assertTrue($comments[1]->relationLoaded('commentable'));
} This example says "load all polymorphic Right now, you can use $feeds = Feed::query()
->whereUser($user)
->with('comments.commentable')
->get();
$feeds->loadMorph('comments.commentable', [ // Nested relations don't work here
Post::class => 'user'
]); You cannot use That's the issue this PR solves, as you can load the above example like so: $feeds = Feed::query()
->whereUser($user)
->with('comments.commentable', function (MorphTo $morphTo) {
$morphTo->withMorph(Post::class, ['user']);
})
->get(); |
Is it desirable to also/only allow the syntax of Comment::query()
->with(['commentable' => function (MorphTo $morphTo) {
$morphTo->withMorph([Post::class => ['user']]);
}])
->get(); |
@staudenmeir Sounds like a good idea! I changed it accordingly |
Please remove We could rename the I think we should also support single relations like Comment::query()
->with(['commentable' => function (MorphTo $morphTo) {
$morphTo->withMorph([Post::class => 'user']);
}]) ^^^^^^
->get(); |
@staudenmeir I added support for single relations: a6700e5#diff-4c052acf0022213a4cc23f53ba42720eR123 $morphTo->withMorph([Post::class => 'user']); |
Co-Authored-By: Dries Vints <[email protected]>
This has been merged but looks like GitHub didn't mark it as merged because of draft status. Thanks for the contribution! Could you send a PR to the docs when you get a chance? |
PS I renamed the method |
Thanks! I'll send a PR to the docs later this week |
@brendt @taylorotwell morphWithCount? |
Is there way to use it with nested relationships? |
This PR adds support for eagerly loading relations of morphed models, based on their type.
An example:
Comment
has a morph relation tocommentable
toVideo
orPost
Post
has a relation toUser
With this PR, you can load all these specific relation via queries, instead of using
loadMorph
on collections (#23626). Here's what the above example would look like:The advantage of using this approach compared to
loadMorph
, is that nested relations are supported when usingwith
on the query builder.This PR is in draft, because I've got two questions still:
withMorph
to be similar toloadMorph
, though maybe someone has a better idea?Can someone point me into the right direction on how to test this. I'm getting these errors:Edit: figured out the right place to test this.Call to a member function connection()
when runningDatabaseEloquentMorphToTest
on its own, so I'm unable to try things out myself right now.