Laravel 5 middleware to remove/trim empty whitespace from the input request
In this post we will give you information about Laravel 5 middleware to remove/trim empty whitespace from the input request. Hear we will give you detail about Laravel 5 middleware to remove/trim empty whitespace from the input requestAnd how to use it also give you demo for it if it is necessary.
In this Laravel PHP Tutorial, I will let you know how to create custom middleware to strip whitespace from the beginning of the string in the request.
Sometime user searches for empty string and get empty result becauase empty string behave like a characters which is accurate but this is not the good practice to send user input data directly into the query without sanitizing input data.
You can trim all input using the custom middleware in the Laravel.
Create Middleware
You can use the PHP artisan command to create the middleware file to trim request data.
php artisan make:middleware BeforeAutoTrimmer
Update the below code in the newly created middleware file (located at app/Http/Middleware/BeforeAutoTrimmer.php).
<?php namespace AppHttpMiddleware; use Closure; classBeforeAutoTrimmer { publicfunctionhandle($request, Closure $next) { $request->merge(array_map('trim', $request->all())); return$next($request); } }
Update the Laravel Kernel file
Once you have done with your middleware file, you need to tell Laravel application to run middleware on each request or you can applied it for a specific group to trim whitespace from the request data.
app/Http/Kernel.php:
protected $middleware = [ // .. AppHttpMiddlewareBeforeAutoTrimmer::class, ];
You can now try this example to send form with empty input to check the validation.
Hope this code and post will helped you for implement Laravel 5 middleware to remove/trim empty whitespace from the input request. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs