Skip to content

New middlewares in Laravel 5.4 – TrimStrings and ConvertEmptyStringsToNull

Last updated on February 17, 2018

Laravel 5.4 shipped with two new middlewares called TrimStrings and ConvertEmptyStringsToNull

TrimStrings middleware – automatically trim all the request data.
ConvertEmptyStringsToNull middleware- automatically convert all strings to null.

Trim Strings Middleware

As the name implies, this middleware removes extra spaces from the beginning and ending from request data automatically. For example, if a user submits something like this.

dd(request('email'));
// '[email protected] '

With the presence of this middleware (\Illuminate\Foundation\Http\Middleware\TrimStrings::class) in App/Kernel.php, it gets converted into.

dd(request('email'));
// It'[email protected]'

Its removed extra space.

ConvertEmptyStringsToNull

Just as the name suggests, this middleware converts empty strings to null. If a user submits an empty form instead of getting '' it gets converted to null.

If the user submits a form with a not required field that maps to the nullable database column.

dd(request('email'));
// ' '

With the presence of this middleware (\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class) in App/Kernel.php, it gets converted into.

dd(request('email'));
// null
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments