Yes there is the part of events that consists of a class that generates some or some modifications before the event for example to save, in your code example can be used a creating
so that before creating this record has any modifications, example:
Create a class in the folder app\Observers
which will represent those amendments:
class UserObserve
{
public function creating(User $user) // somente na hora da criação
{
$user->cep = str_replace(['.','-'],'',$user->cep);
}
// OU
// talvez no seu caso o evento é saving porque
// nesse caso é antes de salvar qualquer modificação
public function saving(User $user)
{
$user->cep = str_replace(['.','-'],'',$user->cep);
}
}
Observing: I made two methods, you can use each other, one means in creating this record (creating
) and the other every time before saving (saving
). There is even more: retrieved
, creating
, created
, updating
, updated
, saving
, saved
, deleting
, deleted
, restoring
and restored
.
In the AppServiceProvider
that’s in the folder app\Providers
add in the method boot
the following code snippet:
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
User::observe(UserObserve::class);
}
public function register()
{
}
}
Another example:
Reference:
What version of Laravel?
– novic
The Laravel version is 5.7
– adventistaam
Possible duplicate of Eloquent Laravel - Column user_id
– novic