DEV Community

TechzPad
TechzPad

Posted on

Sanitize & Formatting Data in Laravel with Transformer Package

The transformer is a PHP package deal for sanitizing and formatting data powered by Laravel’s validation components. The package deal uses a familiar Laravel validation-like syntax to remodel data utilizing callable functions, classes, and more:
`use Closure;

// Example available functions at runtime:
function to_carbon($value)
{
return new Carbon\Carbon($value);
}

function only_numbers($value)
{
return preg_replace("/[^0-9]/",'',$value);
}

$input = [
'first_name' => ' Tilak ',
'last_name' => ' KC',
'phone_number' => '987-587-2588',
'date_of_birth' => "1989-05-01",
];

(new DataTransformer($input, [
'first_name' => 'trim|ucfirst',
'last_name' => 'trim|ucfirst',
'phone_number' => 'only_numbers',
'date_of_birth' => 'to_carbon|->format:m/d/y',
]))->transform();`

Top comments (0)