Created
March 18, 2019 16:29
-
-
Save bumbummen99/a5242573608e178bcefb46b1ffe981aa to your computer and use it in GitHub Desktop.
Converts Strings like 'true', 'on' and 'false' to boolean on Requests in Laravel.
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 | |
/** | |
* Based on https://github.com/laravel/ideas/issues/514#issuecomment-299038674 | |
*/ | |
namespace App\Http\Middleware; | |
use Illuminate\Foundation\Http\Middleware\TransformsRequest; | |
class ConvertStringBooleans extends TransformsRequest | |
{ | |
protected function transform($key, $value) | |
{ | |
$val = strtolower($value); | |
if ($val === 'true' || $val === 'on') { | |
return true; | |
} | |
if ($val === 'false') { | |
return false; | |
} | |
return $value; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment