@petrogromovo take a look at this post:
https://dev.to/samolabams/transforming-laravel-request-data-using-middleware-2k7j
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello, In Laravel 5.8 app submitting form with the data I need to clear data, like doubled spaces, so I created middleware with clearing code :
app/Http/Middleware/WorkTextString.php :
<?php
namespace App\Http\Middleware;
use Closure;
use App\Http\Traits\funcsTrait;
use function PHPSTORM_META\type;
class WorkTextString
{
use funcsTrait;
public function handle($request, Closure $next, $strip_tags_excluding= false )
{
$inputDataArray = $request->all();
\Log::info($request->all());
$stripTagsExcludingArray= $this->pregSplit('/ /',$strip_tags_excluding);
foreach( $inputDataArray as $next_field_name=>$next_field_value ) {
if ( !empty($next_field_value) and is_string($next_field_value) ) {
$skip_strip_tags= in_array($next_field_name,$stripTagsExcludingArray);
$inputDataArray[$next_field_name] = $this->workTextString($next_field_value, $skip_strip_tags);
}
}
\Log::info('$inputDataArray:: ::'); // I CHECK AND SEE CLEARED DATA!
\Log::info($inputDataArray);
$request->replace($inputDataArray); // THAT DOWS NOT WORK ?
// $request->merge($inputDataArray); // ALSO I TRIED THIS WAY - does not work
return $next($request);
}
}
But I see that submitted data are not cleared. Lokks like $request->replace does not work for me...
in routes/api.php :
Route::resource('skills', 'API\Admin\SkillController')->middleware('WorkTextString');
How correctly ?
I used set attributes for that instead.
public function setTitleAttribute($value)
{
$this->attributes['title'] = ucwords(strtolower($value));
}
It transforms the value before insert and update to the proper format.
Please or to participate in this conversation.