Skip to content
Advertisement

Laravel ORM is giving an incorrect result on a simple query

I really don’t get it, help me understand.

I’m writing an app which among other things calculates a NFL team “mark” (a mark is Win/Lose/Tie, sorry I don’t know the word in English for that), so I have a “Marca” attribute in the Team model, looks like this:

JavaScript

Queries are very easy, first I get the number of games where the team is playing as local and calculate wins, loses and ties, for example, this one is for wins:

JavaScript

Then I do the same with games as visitor, but inverting comparison signs, of course.

Now, lets take a look at game Seattle (38) at Atlanta (25), if I do in the database

JavaScript

Of course, it returns 0.

In the ORM, the generated query is:

JavaScript

The thing is returning a 1. I even replaced the ->count()->get() for a ->get() and do a foreach over the results:

JavaScript

The thing is returning a line where it says “25 > 38”

I really don’t understand what happens here. Any idea?

Advertisement

Answer

You can achieve this using whereRaw :

JavaScript

Or as suggested in comments, whereColumn:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement