9

I'm trying to get a value from database using the following code

    $request = Registrationrequest::where('course_id', $id)
                                   ->where('user_id', Auth::user()->id)
                                   ->where('registered', true)->count();

I'm using Laravel. I have a row with course_id = 4, user_id=3 and registered=true. When I var_dump each values separately, I'm confirming it. But when I use this where condition together, I'm not getting the count value as 1, instead getting 0.

I have another query where I use

$request = Registrationrequest::where('course_id', $id)
                                ->where('user_id', Auth::user()->id)
                                ->where('registered', false)->first();

This works perfect when the registered value is false.

Can anybody tell me where am I wrong?

7
  • What is the registered column's type (in the database)? Commented Dec 15, 2014 at 4:34
  • @SverriM.Olsen It's Binary Commented Dec 15, 2014 at 4:34
  • Does it work if you set it to 1 instead of true, and 0 instead of false? Commented Dec 15, 2014 at 4:40
  • No, it's not working. I tried that. Commented Dec 15, 2014 at 4:40
  • I found the problem. When I save the data, I saved registered as 'true' or 'false'. It has to be 1 or 0. When I changed all the false and true through out the code with 0 and 1, it worked. Commented Dec 15, 2014 at 4:42

2 Answers 2

4

Change true with 1, and false with 0.

Sign up to request clarification or add additional context in comments.

2 Comments

Normally true and false should work. True is working. But not false case. Anyway, changing it to 1 and 0 works.
It is because they are being cast to string. When boolean values are cast to string they are turned into: true = '1' and false = '' (empty string).
0

Changed all the false and true from the insert, delete and select queries.

$request = Registrationrequest::where('course_id', $id)
                                ->where('user_id', Auth::user()->id)
                                ->where('registered', 0)->first();

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.