1

I'm trying to query postgres db and get a SQL error every time. The query is correct, but why does it now work?

Query

Apireq::where('calls','<','maxcalls')->get();

Error

SQLSTATE[22P02]: invalid input syntax for integer

Field types are both set to bigint.

It's bizzare.

2 Answers 2

2

You need to use whereRaw instead of just where. It's weird, but it gets around the issue which I think is a Laravel Bug.

whereRaw('calls < maxcalls') 
Sign up to request clarification or add additional context in comments.

Comments

2

@Andrew's answer is totally correct, except that it is not a bug but expected behavior.

Here's an example why:


where('foo', '=', 'bar')

Now there are two possibilities how Laravel could interpret (or misinterpret?) this

1. Column named bar

"Sure you want to compare the column foo with bar. Here's your SQL:"

WHERE foo = bar

2. The value "bar"

"Well obviously you want all the records where foo equals "bar". Here you go:"

WHERE foo = "bar"

So Laravel has to make a decision. And because a computer (without artificial intelligence at least) can't possibly know if you want to compare with a value or another column, the developers decided it should always compare with the value (probably because it is the functionality that's needed more)

And as you already know, whereRaw is the solution:

whereRaw('calls < maxcalls')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.