Last active
July 9, 2018 06:59
-
-
Save tdkn/a1e7dad283b87e3009fb2922329da73d to your computer and use it in GitHub Desktop.
A Laravel middleware for transform String to Boolean
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Middleware; | |
use Illuminate\Foundation\Http\Middleware\TransformsRequest; | |
class TransformStringBooleans extends TransformsRequest | |
{ | |
/** | |
* Transform the given value. | |
* | |
* @param string $key | |
* @param mixed $value | |
* @return mixed | |
*/ | |
protected function transform($key, $value) | |
{ | |
$lowercase = strtolower($value); | |
if ($lowercase === 'true') { | |
return true; | |
} else if ($lowercase === 'false') { | |
return false; | |
} | |
return parent::transform($key, $value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment