Laravel How to filter inputs before going to the DataBase?

Viewed 23

Laravel How to filter inputs before going to the DataBase

what shoud i do to filter my inputs before saving to the database? i want it to be uniformed in like this format "Name" a uper case followed by lowercase.

in some cases like when the user register a full caps name i want it to be re format as the example "Name"

1 Answers

there's an especific PHP function to do what you need: ucfirst(). This function turns to Uppercase the first char from a string.

For example:

//If $request->name is 'JoHn' or whatever
$filtered_name = ucfirst($request->name);
//Returns 'John'

If is a case with two names, you can use the ucwords() PHP function (Turn first letter uppercase of every word from a string)

//If $request->name is 'OlivER JAMES'
$filtered_name = ucwords($request->name);
//Returns 'Oliver James'

Hope this help you.

Related