DEV Community

Discussion on: Transforming Laravel Request Data Using Middleware

Collapse
 
samolabams profile image
Samuel Tope • Edited

Yeah, you can do that too, just change the cleanData function of your middleware


/**
    * Check the parameters and remove the initial underscore
    *
    * @param  array  $data
    * @return array
    */
    private function cleanData(array $data)
    {
        return collect($data)->mapWithKeys(function($value, $key) {
                if ($key !== '_token') {
                    $key = preg_replace("/(^_)/", '', $key);
                }
                return [$key => $value];
        })->all();
    }

What am doing here is to remove the initial underscore if the request key is not '_token', since laravel needs that for csrf (in case you are using the web route group). Sorry for the late response. Let me know if it helps.