title: Modify request in middleware?
body: <p>I have a TrimInput middleware registrated as a middleware for my routes to trim all user input before the request hits the controller. Within the middleware the trimming seems working, but when I dump the request in the action the request seems unmodified like there was no middleware before.</p>
  
  <p><del>What's the problem here?</del> The problem is the ClientRequest but why?</p>
  
  <pre><code>// TrimInput.php
  &lt;?php namespace App\Http\Middleware;
  
  use Closure;
  
  class TrimInput {
  
      /**
       * Handle an incoming request.
       *
       * @param  \Illuminate\Http\Request $request
       * @param  \Closure $next
       *
       * @return mixed
       */
      public function handle($request, Closure $next) {
          $request-&gt;replace($this-&gt;trimArrayRecursive($request-&gt;all()));
          // When I dump $request right here, all seems fine (the input is trimmed)
  
          return $next($request);
      }
  
      protected function trimArrayRecursive($input) {
          if (!is_array($input)) {
              return trim($input);
          }
  
          return array_map([$this, 'trimArrayRecursive'], $input);
      }
  
  }
  
  
  // Somwhere in my routes.php
  Route::post('/test', ['middleware' =&gt; 'trim', 'uses' =&gt; function(\App\Http\Requests\ClientRequest $request) {
      dd($request-&gt;all()); // Unfortunately dumps the unfiltered (untrimmed) input
  }]); 
  </code></pre>
  
  <p><strong>EDIT:</strong> It turned out, that the above code is working, but unfortunately my <code>ClientRequest</code> ignores the <code>TrimInputMiddleware</code>.</p>
  
  <pre><code>// ClientRequest.php
  &lt;?php namespace App\Http\Requests;
  
  class ClientRequest extends Request {
  
      /**
       * Determine if the user is authorized to make this request.
       *
       * @return bool
       */
      public function authorize() {
          return true;
      }
  
      /**
       * Get the validation rules that apply to the request.
       *
       * @return array
       */
      public function rules() {
          $idToIgnore = $this-&gt;input('id');
  
          return [
              'name'           =&gt; 'required|max:255|unique:clients,name,' . $idToIgnore,
              'street'         =&gt; 'required|max:255',
              'postal_code'    =&gt; 'required|digits:5',
              'city'           =&gt; 'required|max:255',
              'contact_person' =&gt; 'required|max:255'
          ];
      }
  
  }
  </code></pre>