Comments (11)
Ok, it should be good. I added some tests for hybrid whereHas
with
and everything is passing as of this commit. https://github.com/Tucker-Eric/laravel-mongodb/commit/818d7f66e1468b4c5ba8601474d585a082958dbf
from eloquentfilter.
Skimming over the other library it looks like the issue may be in the addHasWhere()
method that library overwrites returns a whereIn()
instead of a whereExists()
or whereCountQuery()
.
What version of Laravel are you using?
Do you get the same error running the following without the filters?
User::whereHas('profile', function($query) {
return $query->where('gender', 'male');
})->get();
from eloquentfilter.
I think you're on the right track! I'm using Laravel 5.4. And yes I do get the same error running the code you supplied
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'exists ? and `gender` = ?)' at line 1 (SQL: select * from `users` where exists (select `user_id` from `profile` where `user_id` exists 1 and `gender` = male))
from eloquentfilter.
It looks like they should override the has()
method instead because that's where the exists
constraint is being used. That needs to be a whereIn()
or nested whereIn()
if using whereHas()
instead. If that is done correctly the filters would work between databases. All non-joined relations in the filters are queried using the whereHas()
method which in turn uses the has
method under the hood.
from eloquentfilter.
well.. upon doing a little more digging on the very popular package https://github.com/jenssegers/laravel-mongodb/issues?utf8=%E2%9C%93&q=wherehas
It looks like having the "wherehas" working properly for hybrid relations has been a repeatedly requested feature going all the way back to 2014.. and the owner himself has said that implementing this seems very difficult.. and people continue to ask for it.. myself included.
I think unfortunately it must not be easy to make it work at all.. or in any case no one seems to know how to do it. If you think you have an idea of how to make it work and wanted to give it a try at adding a PR there.. I think that would make you a real hero ;)
from eloquentfilter.
I threw together a rough patch for it. You wanna give it a test spin before I submit the pr?
https://github.com/Tucker-Eric/laravel-mongodb/commit/266305caeed70cf8ee787bcd0140c9fca2f0bcd6
from eloquentfilter.
absolutely! will test it today and let you know if I run into any troubles.
wow thanks for taking this on!! I will support your PR as I'm sure others will.. it's been a long time coming!
Does using this branch also automagically makes your EloquentFilter work on mysql->mongo hybrid relations?
from eloquentfilter.
Yup! Although I haven't tested the 2 together, there isn't anything that should break it in this branch. This will also work mongo->mysql relationships too. It just won't support belongsToMany()
because that'sā queried with an inner join.
from eloquentfilter.
Alright alright! I pulled in your branch and am glad I did because I found a problem.
On the one hand, this query which used to fail now works!
$query = User::whereHas('profile',
function ($query) {
$query->where('someBoolField', true);
})->with([
'profile' => function ($query) use ($profileFields) {
$query->select($profileFields);
},
]);
So that's a win! Awesome!
On the other hand, this query which worked before, now fails!
$this->data['matches'] = User::whereHas('matches',
function ($query) {
$query->where('user_id_1', \Auth::id())
->orwhere('user_id_2', \Auth::id());
})->with([
'profile' => function ($query) use ($profileFields) {
$query->select($profileFields);
},
])->get($userFields)
->except(\Auth::id());
Where User and Match are mysql models and Profile is a mongodb model
So it seems that your fix has corrected the mysql->whereHas(mongodb)->with(mongodb) but is now interfering with the scenario mysql->whereHas(mysql)->with(mongodb)
this is my user model in case it's helpful
<?php
namespace App;
use EloquentFilter\Filterable;
use Jenssegers\Mongodb\Eloquent\HybridRelations;
use Laravel\Spark\User as SparkUser;
class User extends SparkUser
{
use HybridRelations, Filterable;
public function modelFilter()
{
return $this->provideFilter(\Omitted\Models\Filters\UserFilter::class);
}
public function profile()
{
return $this->hasOne('\Omitted\Models\Profile');
}
public function matches()
{
return $this->hasMany('\Omitted\Models\Match', 'user_id_1');
}
}
Let me know if you need me to provide more info
from eloquentfilter.
Seems to work beautifully!!
Tried the eloquent filter as well
$input = [
'gender' => 'female',
];
$users = User::filter($input)->with('profile');
That works perfectly too!
And re-tried the two scenarios that I mentioned above and they now both work as expected!
That's some really fantastic work you've done here! Thank you so much! I'll be using your branch for now.
I hope @jenssegers approves your PR quickly! I know many of us have been waiting for has() and whereHas() to be added to his library for a long time so you're going to make a lot of happy people with this!
You rock :)
from eloquentfilter.
I'm going to close this for now. We can reopen if there are issues with that pr.
from eloquentfilter.
Related Issues (20)
- Combine filter by related and own fields HOT 4
- Postgresql support (case-insensitively) HOT 2
- Filter on append attributes HOT 1
- sorted by table with relation HOT 1
- Breaking change on 2.4.1 from 2.4.0 HOT 3
- Add orWhereLike HOT 2
- How should you handle Laravel Scout integration? HOT 8
- filter by created_at HOT 1
- Your composer is powerful! HOT 1
- Access the other request parameters within the filter function? HOT 2
- Access model class HOT 2
- filtering by date interval HOT 1
- Filter by column and and relation column HOT 1
- Related filter does not accept zero or false value
- $relations setting to create orWhereHas HOT 4
- Can't sort many to many HOT 1
- Can't sort on date HOT 1
- Filter by relationship field and latest fails HOT 3
- Relations with one related method HOT 3
- Filtering by deep relation HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
š Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ššš
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ā¤ļø Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from eloquentfilter.