fsdolphin's avatar

What is the proper way to check a boolean from a DB

Hi,

I'm trying to check if a boolean is true or false, I get true when it is set to true (1) but I don't get anything when it is set to false (0).

The boolean field is called showBanner and I'm checking it as follow...

$banners = \App\Banner::where('showBanner', '1')->get(['showBanner']);

foreach($banners as $banner){
    $result = $banner->showBanner;
    if($result == 1){
        return "It's TRUE";
    }
    else{
        return "It's FALSE";
    }
}

What is the proper way to check booleans?

Also, I'm not even sure if the way I'm retrieving the field is the most efficient way.

FYI - I also tried if($result == true) and if($result == "1")

Thanks

0 likes
15 replies
fsdolphin's avatar

From the documentation

For example, let's cast the is_admin attribute, which is stored in our database as an integer (0 or 1) to a boolean value:

@skliche If I understand the documentation correctly I should store my boolean as an int and then do the casting when the value is retrieved. Does this means that there is not way to evaluate a boolean value coming from a DB?

Thanks

skliche's avatar

@fsdolphin The database usually stores the boolean value as an integer, e.g. TINYINT(1). So you retrieve an integer from the database, not a boolean. The attribute casting performs automatic casting to/from boolean so you can work with the value as if it was stored as a boolean. Sorry if this sounds confusing ...

It should only make a difference when you use === comparisons.

The following should work even without attribute casting:

if ( $result == true ) {
   return "It's TRUE";
}
return "It's FALSE";

You can always do a dd( $result ) after $result = $banner->showBanner; to see what it really contains.

1 like
fsdolphin's avatar

@skliche Thank you for the info.

Well, in theory the code below should work but the "It's FALSE" statement never triggers.

if ( $result == true ) {
     return "It's TRUE";
}
return "It's FALSE";

If I return $result it shows a "1" in the browser when set to true, but it doesn't show anything when it is set to false.

skliche's avatar
skliche
Best Answer
Level 42

@fsdolphin No surprise there. The query in your example only includes records where that column contains 1... where('showBanner', '1'). And it should also be the case for where('showBanner', true).

3 likes
fsdolphin's avatar

@skliche It works either way, I tried where('showBanner', '1') and where('showBanner', true) and both worked, but this pointed me to what I think is my problem.

The issue with my code is that I'm only looking for the columns where showBanner is true (1) so when I set it to false (0) nothing is retrieved.

Now the question is... is there a way to say give me both, the columns where showBanner is true OR false?

I know I could add multiple where clauses like this...

$banners = \App\Banner::where('showBanner', false)->where('showBanner', true)->get(['showBanner']);

But this will never give me anything since I'm asking for false AND true and what I need false OR true.

Is there an OR clause?

Thanks

fsdolphin's avatar

Got it. The orWhere clause!

$banners = \App\Banner::where('showBanner', false)->orWhere('showBanner', true)->get(['showBanner']);
skliche's avatar

@fsdolphin Yes, as I said, your query only returns those with the value 1.

To get all records use

$banners = \App\Banner::all( ['showBanner'] );

If you want or then you would use orWhere() on the second clause. But the above is what you want.

1 like
fsdolphin's avatar

@skliche Yes, you first statement is what helped me find my issue.

The query in your example only includes records where that column contains 1...

What wasn't clear was that fact that you said 1 and true which is the same thing and I already had.

where('showBanner', '1'). And it should also be the case for where('showBanner', true)

Thanks a lo for the simplified query

$banners = \App\Banner::all( ['showBanner'] );

Thanks a lot for all of your help, you are awsome.

skliche's avatar

@fsdolphin You are welcome. I was a bit confused at the beginning until I realized what the problem really was. I hope I did not confuse you as well as you don't need attribute casting in this case :-)

1 like
Snapey's avatar

if you consider unset (Null) in the database as being false then you don't need the where clause at all !

As for checking, I just use if(value){...}

$banners = \App\Banner::get();

foreach($banners as $banner){
    if($banner->showbanner){
        return "It's TRUE";
    } else {
        return "It's FALSE";
    }
}
1 like
fsdolphin's avatar

@Snapey Thank you for the simplified code. Question, is one method more efficient than the other? The reason I'm asking is because in your code we are getting everything with the get() method and using the where clause we are only getting a specific cells.

$banners = \App\Banner::get();

vs

$banners = \App\Banner::where('showBanner', false)->orWhere('showBanner', true)->get(['showBanner']);
Snapey's avatar

If you only want the one column then you can still get(['showBanner'])

1 like
Snapey's avatar

I see no point in this

$banners = \App\Banner::where('showBanner', false)->orWhere('showBanner', true)->get(['showBanner']);

You might as well

$banners = \App\Banner::where(1,'=',1)->get(['showBanner']);
2 likes

Please or to participate in this conversation.