Skip to content

Instantly share code, notes, and snippets.

@afiqiqmal
Last active December 27, 2020 03:13
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 afiqiqmal/ca07ae3dad0be70e9691ce861633c236 to your computer and use it in GitHub Desktop.
Save afiqiqmal/ca07ae3dad0be70e9691ce861633c236 to your computer and use it in GitHub Desktop.
Middleware of Laravel to Convert Incoming Request contain boolean string to boolean. Kinda head ache when laravel didn't detect boolean string
<?php
class BooleanStringConverter extends TransformsRequest
{
protected function transform($key, $value)
{
if (in_array($key, $this->except, true)) {
return $value;
}
if (is_string($value)) {
if ($value == 'true') {
return true;
}
if ($value == 'false') {
return false;
}
}
return $value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment