Laravel 5.4 Includes Two New Middleware
Laravel 5.4 / January 18, 2017

Laravel 5.4 Includes Two New Middleware

Laravel 5.4 is scheduled to be released next week, and it already includes many great new features, but the team was able to sneak in two new Middleware offerings. These are TrimStrings and ConvertEmptyStringsToNull.

Trim Strings Middleware

Just as the name suggests, the Trim Strings middleware will automatically trim all the request data so you don’t have to worry about those situations where someone may inadvertently add extra spaces in your form fields.

As an example let’s say you have a newsletter signup which requires an email address. Previously, if someone entered an extra space it would end up like this:

// ' '

Now, by adding the TrimStrings middleware to App/Kernel.php:

protected $middleware = [

This would result in:

// ''

Convert Empty Strings to Null

Paired with the Trim Strings, is a new ConvertEmptyStringsToNull which works in basically the same way. A good example is a form with a not required field that maps to a nullable database column.

As an example, pretend you have a VAT field for users to enter their tax code.

<input type="text" name="vat" value="">

If you left it blank prior, it would return an empty string:

// ''

With the ConvertEmptyStringsToNull middleware this will now be null:

// null

Be sure and check out the Laravel 5.4 page to see other new features coming to this release and also join the newsletter to not miss out on new posts.

Laravel News Partners


Join the weekly newsletter and never miss out on new tips, tutorials, and more.