Skip to content

Instantly share code, notes, and snippets.

@bumbummen99
Created March 18, 2019 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bumbummen99/a5242573608e178bcefb46b1ffe981aa to your computer and use it in GitHub Desktop.
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.
<?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