@fsdolphin You could use attribute casting: http://laravel.com/docs/5.1/eloquent-mutators#attribute-casting. The documentation includes an example for boolean values.
Aug 30, 2015
15
Level 5
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
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
Please or to participate in this conversation.