1
Which would be more recommended to use when manipulating a string that in this case would be (,) for (.) when inserting a value in the database (Mysql) of decimal type in Laravel?
1 - Using a custom middleware
class convertCommaInDot extends TransformsRequest
{
protected function transform($key, $value)
{
return str_replace(',','.',$value);
}
}
Calling middleware in the route/web.php file
Route::post('/numeros/add', 'NumeroControlador@store')->name('number.store')
->middleware('CommaInDot');
OR
2 - Using the setNumeroAttribute($value) mutator, in this case you do not need to use middleware.
class Numero extends Model
{
public function getNumeroAttribute($value)
{
return $this->attributes['numero'] = str_replace('.',',',$value);
}
public function setNumeroAttribute($value)
{
$this->attributes['numero'] = str_replace(',','.',$value);
}
}
Both generate the same result, that is, I insert in the form a number 1,23 and automatically the value is transformed to 1.23
Mutator is for changing given; middleware is for handling HTTP messages. So, mutator is the answer.
– Woss
Igor out of curiosity this Tranformesrequest is a package?
– novic
In version 6 there is
middleware
that takes out text space, for example, so I see so what the best way will depend on because theframework
opened a gap ! its version is the 5 or 6 (specifies certinho)– novic
@Virgilionovic About Transformsrequest, it is a class that Laravel uses to create Convertemptystringstonull middleware.
– Igor AC
@Virgilionovic I use the Laravel 6.
– Igor AC
Yes I ended up finding ... I think your question can be asked in both ways the Laravel has it, but of course with settings and an appropriate code
– novic
Thank you for the reply @Andersoncarloswoss , thank you.
– Igor AC
@Virgilionovic hm, got it, thanks for collaborating bro!!
– Igor AC
Because so Igor this middleware you can do by routes (verb
post
,put
) where this route has data that will be formatted/validated in its own way. (It’s an initial idea, I’m on the street, but tonight I’ll take a look)– novic