-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
Can't get doesntHaveMorph to work #29138
Comments
Okay, maybe my understanding of the function is completely wrong, because it seems the behavior would match that of what the test is doing. https://github.com/laravel/framework/pull/28928/files#diff-189f859ae631c6808f0f151fbaf1f985R170
So the Then
But isn't it supposed to return all the If that is not the idea with this function, Is there any way for me to then get all the Comments that don't belong to a Basically, I was expecting something like this from the
|
This is indeed the correct behavior, but I can see how it's not the most intuitive.
Possible solutions for your situation:
Link::hasMorph('linkable', [LicensePeriod::class, Product::class])->get();
Link::where('linkable_type', '!=', Subscription::class)->get();
$types = (new Link)->newModelQuery()->where('linkable_type', '!=', Subscription::class)
->distinct()->pluck('linkable_type')->all();
Link::hasMorph('linkable', $types)->get(); |
@staudenmeir I see. Yes, at least I found it slightly counter intuitive but now I know. :) It would be cool to be able to do the other thing as well though! Thank you for the suggestions! |
@staudenmeir On second thought, I think the function name could either be changed, or the documentation updated to better explain what you explained to me. Right now Also, the docs only mention:
If we were to match the behaviour it would be:
plus the |
The idea behind the current implementation is that Laravel can get the possible types for you, but you can also provide them yourself. With your suggestion, Laravel would always have to execute an additional query.
I don't think we have to list all methods, "corresponding methods" should cover them.
An example with |
Feel free to send in a pr to the docs, thanks. |
Description:
I cannot seem to get the new
doesntHaveMorph
to work.whereHasMorph
works fine though.Steps To Reproduce:
Simple example:
I have a
Link
model and different models can have links (e.g. subscription, license period, product).This is the relation on the Link model:
I have seeded these 3 links to the database: (posting a picture since it's easier to look at)
Then the code using
doesntHaveMorph
:Link::doesntHaveMorph('linkable', [Subscription::class])->get()
-> returns an empty collection, where I expect to get the links for the License period and Product.
On the other hand
Link::whereHasMorph('linkable', [Subscription::class])->get()
-> correctly returns a collection with one item -> the link for the Subscription.
The text was updated successfully, but these errors were encountered: