Skip to content

Instantly share code, notes, and snippets.

@tdkn
Last active July 9, 2018 06:59
Show Gist options
  • Save tdkn/a1e7dad283b87e3009fb2922329da73d to your computer and use it in GitHub Desktop.
Save tdkn/a1e7dad283b87e3009fb2922329da73d to your computer and use it in GitHub Desktop.
A Laravel middleware for transform String to Boolean
<?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