Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

OrWhere is return wrong result #3337

Answered by GromNaN
vipinpapnai asked this question in Q&A
Discussion options

I have a query

self::select($selected_field)
 ->where(['status'=>true,'reporting_method'=> 3])
 ->orWhere(['status'=>true,'reporting_method'=> '3'])
 ->get();

it should return results where reporting_method is either 3 or '3' with status: true
But it returns all status: true records
Here is the dump of QueryLog

array:1 [
 0 => array:3 [
 "query" => "{ "find" :"reporting_method", "filter" : { "$or" : [ { "$and" : [ { "status" : true }, { "reporting_method" : { "$numberInt" : "3" } } ] }, { "$or" : [ { "status" : true }, { "reporting_method" : "3" } ] } ] }, "projection" : { "_id" : true, "name" : true, "reporting_method" : true } }"
 "bindings" => []
 "time" => 234
 ]
]

It was working well and good in Lumen 5 but in lumen 10 it is not working well.
Please check

You must be logged in to vote

By using an array of criteria, Laravel splits them into a list of "where" clauses.
Without the MongoDB override, the SQL generated by your query is the following:

select * from "users" where ("status" = ? and "reporting_method" = ?) or ("status" = ? or "reporting_method" = ?)

The query generated by the MongoDB Query Builder is consistent with the Laravel behavior:

{
 "$or": [
 {
 "$and": [
 {
 "status": true
 },
 {
 "reporting_method": 3
 }
 ]
 },
 {
 "$or": [
 {
 "status": true
 },
 ...

Replies: 2 comments 1 reply

Comment options

By using an array of criteria, Laravel splits them into a list of "where" clauses.
Without the MongoDB override, the SQL generated by your query is the following:

select * from "users" where ("status" = ? and "reporting_method" = ?) or ("status" = ? or "reporting_method" = ?)

The query generated by the MongoDB Query Builder is consistent with the Laravel behavior:

{
 "$or": [
 {
 "$and": [
 {
 "status": true
 },
 {
 "reporting_method": 3
 }
 ]
 },
 {
 "$or": [
 {
 "status": true
 },
 {
 "reporting_method": "3"
 }
 ]
 }
 ]
}

In order to get a $and in the second criteria, you have to create a nested subquery:

self::select($selected_field)
 ->where(['status' => true, 'reporting_method' => 3])
 ->orWhere(fn (Builder $builder) => $builder->where(['status' => true, 'reporting_method' => '3']))
 ->get();

You can also write all the query in the same where:

self::select($selected_field)
 ->where(['$or' => [
 ['status' => true, 'reporting_method' => 3]],
 ['status' => true, 'reporting_method' => '3']],
 ]])
 ->get();

And finally, for this specific case, you can use a $in operator:

self::select($selected_field)
 ->where('status', '=', true)
 ->where('reporting_method', 'in', [3, '3'])
 ->get();
You must be logged in to vote
0 replies
Answer selected by GromNaN
Comment options

Ok Thanks, but i need to change my code at all orWhere Occurance.
Anyway, Thanks for the help

You must be logged in to vote
1 reply
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /