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

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

app/Http/Kernel.php:
protected $middleware = [
        // ..
        \App\Http\Middleware\BeforeAutoTrimmer::class,
    ];

Phone: (+91) 8800417876
Noida, 201301
ataşehir escort maltepe escort bostancı escort kadıköy escort maltepe escort ataşehir escort tuzla escort pendik escort ümraniye escort kartal escort
pendik escort
izmir escort izmir escort izmir escort izmir escort denizli escort antalya escort antalya escort antalya escort izmir escort izmir escort izmir escort izmir escort izmir escort
hatay escort nevsehir escort nigde escort kirikkale escort batman escort osmaniye escort kirklareli escort tokat escort rize escort adiyaman escort gumushane escort batman escort
porno izle
sohbet okey oyna
cialis satış cialis fiyat cialis 20 mg cialis 100 mg cialis 5 mg fiyat

viagra fiyatı

viagra 100 mg fiyat

viagra sipariş

orjinal viagra

viagra satın al

izmir escort