0

Why when I pass false to Model::where() and then trying to get first() element it always returns me first element instead of null?

Example:

I have Referrer model with hash_id = 123aSd456fGh

and I try to get this like below:

$requestReferrer = false;

$referrer = Referrer::where('hash_id', $requestReferrer)->first();
dd($referrer); // returns first Referrer model with hash_id = 123aSd456fGh

FYI

when I pass null in exchange for false it returns null.

3
  • share table data for Referrer ? Commented Jul 5, 2019 at 8:26
  • Why are you using false? It seems null would be most suitable for this use case? Then you can do ->whereNull('hash_id') . Commented Jul 5, 2019 at 9:02
  • $requestReferrer cames form HTTP request and I don't know what value it has. Anyway. The main question is why it works like that and can I prevent these situations in the feature. Commented Jul 5, 2019 at 9:07

1 Answer 1

1

Reason: SQL Server will automatically change the bit value to the varchar value of true or false.

The following works there:

$requestReferrer=false;
$referrer = Referrer::where('hash_id', "$requestReferrer")->first();

Or

$requestReferrer = 'false';
$referrer=Referrer::where('hash_id',$requestReferrer)->first();
Sign up to request clarification or add additional context in comments.

2 Comments

It will work but I wonder why it works like that and can I prevent these situations in the feature. Passing null works too, it's not the point
I have updated my answer. For clarity check the same where condition you have used with different column of datatype interger/decimal etc

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.