Perform action before saving?

Asked

Viewed 155 times

3

I’m getting the requests form and putting everything at once to save using the protected $fillable = ['nome', 'idade', 'cep', 'nr_casa']; of Laravel

I’m getting it like this:

public function save(Request $request)
{
   $user = new User( $request->all() );
   $user->save();
}

The cep typed in the input that’s how it is: 69.084-120 and would like before saving it to 69084120.

Is there any function beforeSave in the Laravel as well as the Yii?

My table is

nome varchar(100)
idade int
cep varchar(8)
nr_casa varchar(10)

2 answers

3


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:

  • In case, I will have to create the Events folder first, this is it or I can use php Artisan?

  • @adventistaam yes, take a look at the other example best explained: https://answall.com/questions/297038/eloquent-laravel-coluna-user-id/297053#297053

  • @adventistaam made some changes ...

  • 1

    You have to do all the normal Imports, is that it? Because it hasn’t worked yet

  • Yes of course all imports @adventistaam which did not work error?

  • As I set the field limit to 08, he keeps putting the dots and dash

Show 1 more comment

1

The Laravel already has a structure already foreseen for this in the models, are the mutators: https://laravel.com/docs/5.7/eloquent-mutators in this case, you can use:

public function setCepAttribute($value) {
    $this->attributes['cep'] = str_replace(['.','-'],['',''],($value);
}

in the same way that you can also put the dots and dashes back when searching for the value by creating the function

public function getCepAttribute($value) {
   return TRATAR O VALOR AQUI.
}
  • That inside the model?

  • yes, just create the functions with the name of the attribute you want to modify.

  • It gave problem in the column nr_casa that is varchar

Browser other questions tagged

You are not signed in. Login or sign up in order to post.