If there is an event part that is made up of a class that generates some or some modifications before the event, eg save, in your code example, a creating
can be used so that before creating this record there is some Example :
Create a class in the app\Observers
folder that will represent those changes:
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);
}
}
Note: I made two methods, you can use one another, one means when creating this record ( creating
) and the other every time before saving (% w / w). There is even more: saving
, retrieved
, creating
, created
, updating
, updated
, saving
, saved
, deleting
, deleted
and restoring
.
In% w_that is in the restored
folder, add the following code snippet in the AppServiceProvider
method:
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
User::observe(UserObserve::class);
}
public function register()
{
}
}
Another example: