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
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:
dd(request('email')); // 'firstname.lastname@example.org '
Now, by adding the TrimStrings middleware to App/Kernel.php:
protected $middleware = [ \Illuminate\Foundation\Http\Middleware\TrimStrings::class,
This would result in:
dd(request('email')); // 'email@example.com'
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:
dd(request('vat')); // ''
ConvertEmptyStringsToNull middleware this will now be null:
Filed in: Laravel 5.4 / Middleware
dd(request('vat')); // null
Join 31,000+ others and never miss out on new tips, tutorials, and more.
Automatically Making Monthly Payments with Dusk
Laravel Dusk is a new feature coming to Laravel 5.4 and even though that release isn’t out, people have started…
Laracon EU 2017 is Announced
The Laracon EU has just announced the dates and early bird ticket pricing for the 2017 conference. This year it will…