This repository has been archived by the owner on Sep 9, 2021. It is now read-only.
Add POC for cross-server whereHas/whereDoesntHave #17
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hi.
I just learned about cross-database whereHas earlier today. I always assumed that it was impossible, so it was a nice surprise. But I was quickly disappointed when I realized it only works when the database connections are on the same server. I mean, it's kind of obvious that it has to be that way, but I had hoped I could use it in a project where I get Users from an old remote WordPress server (using Corcel).
Just for fun I tried messing around with the QueryBuilder, and I think I might have found a workaround to perform whereHas queries on relationships that are on a completely separate DB servers. But it's the first time I've ever hacked on the QueryBuilder, so I might have broken stuff that I'm not even aware of.
...So I decided to write this PR with a proof of concept, hoping that you could maybe take a look and see if it would make sense to continue working on it, and eventually merge it into your package?
The way it works is quite simple. In the
Builder::has()
method, I check if the two models are hosted on different servers. If they are, using a subquery won't work, so in stead of calling youraddHasWhere()
, I perform a workaround:doesntHave()
, in which case null values are also allowed).I realize this hack doesn't scale very well, but I don't know - maybe it's better than nothing.