For Laravel post request’s parameter get with $request->all();
This function itself does trimming for the request parameter if there is any preceding space.
I try to use $_POST
instead of $this->request->all()
; it is not trimming any request parameter and works as expected.
$_POST
is not safe to use.
please share suggestions/ideas how can I do that without trimming any value ?
2
Answers
You have HTTP middlewares, one of those trims the data. Check this: https://github.com/laravel/laravel/blob/9ae75b58a1ffc00ad36bf1e877fe2bf9ec601b82/app/Http/Kernel.php#L22
You can see there is a
AppHttpMiddlewareTrimStrings::class
, you have to remove that one.The trimming of your input value happens to be due to an active global middleware. The
AppHttpMiddlewareTrimStrings
middleware to be exact.Since you say you do not want to disable the middleware for your entire application, you should look into disabling it only for a specific route or routes. You can do so in two ways:
However, please note that the
withoutMiddleware
method can only remove route middleware and does not apply to global middleware. Since theTrimStrings
is by default a global middlewere, you will still need to remove it from the$middleware
array in theapp/Http/Kernel.php
file and add theTrimStrings
middleware manually again for all routes where you do not want to remove it. To do so, use a similar looking route group syntax as demonstrated in option 2, like so:You can also take a look at the documentation: https://laravel.com/docs/10.x/middleware#excluding-middleware