Laravel middleware to remove/trim empty whitespace from the input request

Laravel middleware to remove/trim empty whitespace from the input request

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).

namespace App\Http\Middleware;
use Closure;

class BeforeAutoTrimmer {
    public function handle($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.

protected $middleware = [
        // ..

Phone: (+91) 8800417876
Noida, 201301
sakarya escort sakarya escort sakarya escort sakarya escort serdivan escort izmir escort eporner
akyazı escort sakarya escort ferizli escort sakarya escort geyve escort sakarya escort hendek escort sakarya escort karasu escort sakarya escort sapanca escort serdivan escort webmaster forum ankara travesti