-
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
whereHas() not working with MorphTo() #5429
Comments
Did you managed to get this working? I'm having the same problem. |
Yep, having the same problem! |
same here |
Running into the same issue. It doesn't even seem to look at the morph type, it just does a Any more information on it? |
I have the same problem. Very sad. |
I ran the code: $tasksQuery->whereHas('taskable', function ($q) use ($search) {
$q->where('title', 'like', "%{$search}");
}); but it throw an exception. |
I reaallly need this asap, so I'm going to look into it in a few days and try to debug it. I don't know anything about the code, so if anyone could clarify what I should be looking for, go ahead. |
Yup same here. Wonder if this was ever working? |
I think this kinda impossible currently, because if you see the This polymorphism relationship only fully capable when the model instance/object were loaded whereby the You can have a look at file I'm sure @taylorotwell and others core developer were fully aware about this issue, but it might be too hacky to make this possible. Anyway it's just my opinion. |
Looks like this is a no-fix then. |
Err. Any workaround then? |
What about throwing a better exception when it happens? Also would like to On Thu, Feb 5, 2015 at 10:52 AM, Sebastiaan Luca notifications@github.com
|
You could try to have the As you can see the line with variable illuminate/database/Illuminate/Database/Eloquent/Builder.php
By using this the |
Thanks @wajatimur! By using an adjusted version of your code, I can use /**
* Get the "has relation" base query instance.
*
* @param string $relation
* @return \Illuminate\Database\Eloquent\Builder
*/
protected function getHasRelationQuery($relation)
{
return Relation::noConstraints(function() use ($relation)
{
$name = $relation;
$relation = $this->getModel()->$relation();
if (get_class($relation) === 'Illuminate\Database\Eloquent\Relations\MorphTo') {
$lookAhead = $this->getModel()->where($name . '_type', '=', 'App\Models\Auth\Users\Admin')->first();
$relation = $lookAhead->{$name}();
};
return $relation;
});
} Feedback is appreciated. |
Hi @quagh, glad its work! A By not having to scheme deeper on the |
I'm not really following when you mention to put a placeholder in the generated query. Can you elaborate on that? Anyway, I forked the Laravel repo and applied my changes to them. If anyone's interested, check out https://github.com/Quagh/framework/tree/database/fix_whereHas_with_morphTo. Here's an example of how to use it. You just pass a string or array of strings of types it should look for. In my opinion, that's the only workable way the query builder can gather all the necessary elements to apply the $this->whereHas('userable', function ($query) use ($id) {
$query->where('id', '=', $id);
}, '>=', 1, ['App\Models\Contact', 'App\Models\Admin']); Composer.json: "require": {
"laravel/framework": "dev-database/fix_whereHas_with_morphTo as 4.2.17"
},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/Quagh/framework.git"
}
], And this is the query it produces when executing the code above (in my app);
As you see, it wraps a single Trying to run phpunit tests right now to verify it doesn't break anything else. I don't have much experience writing tests myself, so if anyone feels like it, go right ahead :) Again, feedback / testing appreciated.
|
👍 Same here |
Yep! Same here! |
I worked out a stupid solution:
public function scopeCommentableExists($query)
{
return $query->where(function ($query) {
$query->where(function ($query) {
$query->where('commentable_type', 'Post')
->has('post');
})
->orWhere(function ($query) {
$query->where('commentable_type', 'Tutorial')
->has('tutorial');
});
});
} |
Nice try @blankhua, that's a hard-coded recursive query. Anyway can anybody confirm, does this issue still exist in Laravel 5? |
Still having this issue in Laravel 5 |
I'm also seeing the same issue in L5 (5.0.30). Would be great to have an official fix soon as the bugs been around for a while. Thanks to @quagh for the interim solution though - I'll check that out |
We're open to pull requests. :) |
Would love to but it's a level of voodoo slightly above my pay grade! Sorry if I sounded demanding... ; ) |
@GrahamCampbell I think I can do a pull request, though I'd love some feedback on my implementation. It requires an extra variable (and clutters the morph class a bit), but it's the only viable solution I've come up with so far. It works though, which I think is the most important part :) |
@quagh Just send the PR and you'll get feedback 😉 |
Are there any news for the PR? :( |
Pleeease |
I came across this issue today in Laravel 5.5. I'll be joining the workaround crew for now. |
Having had this problem a while ago, we wrote a composer package that extends the Eloquent Builder class and implements a work around for this issue. It's not perfect but it does the trick. https://github.com/rapidwebltd/Improved-Polymorphic-Eloquent-Builder It's currently designed for Laravel 5.1, but shouldn't be too hard to adapt for Laravel 5.5. Contributions welcome! |
I searched how to fix where query with morphed table, and come to here and not solved yet 🥉 |
As a temporary workaround, you could declare a relationship for each type.
And then query the model like this:
It's not perfect but worked for me so I though I should share it in case someone has the same use case. |
Just upgraded to Laravel 5.6, this bug is still present. Can please we re-open this issue? |
Just ran into this issue today as well laravel 5.6.20 |
@flyrmyr this is a wont-fix. There is no non-hacky way of doing this. |
Ran in the same problem today. If there is no fix for now maybe this could be mentioned in the documentation. |
Same problem with 5.6 |
@Braunson yes it works but only for explicit relationships. |
@thisdotvoid's solution works in Laravel 5.6.29 (implemented a few minutes ago). It will be nice to have an official way to do this in a close future :) |
polymorphic relations.... same problem here! they are a pain in my ass... |
Same problem here 5.7.9 |
Same here |
This party never going to end.. |
Laravel 5.8.27 adds |
woooooo ... i hardly can believe this! thank you so much |
stops the warning but not result set |
this works for me. Transaction.php
Reservation.php
Project.php
|
Hello Friends I got solution in Laravel official document pleas check below link. I hope this link will be usefull for you. |
thanks bro you are god |
The whereHas() method for querying a polymorphic relationship doesn't seem to be working.
Here are the relationships:
And here is what I am trying to do:
Result:
Is this possible?
The text was updated successfully, but these errors were encountered: