Is there any way to get the original value of an attribute from a Model after modifying it?

1

I'm using Laravel 5 and would like to know if after modifying an attribute of a model, you can retrieve it.

For example:

 $usuario = Usuario::where(['nome' => 'Wallace'])->first();

 $usuario->nome = 'Guilherme';

In the example above, I modified the nome attribute of the model. I would like to know if you can retrieve the nome attribute. Does Laravel "save" the original value somewhere before saving changes?

Of course I could do this by doing another query, but I do not think that is the best way. So I will not accept answers of the type.

I would like to know if there is any way to retrieve the initial value of the "name" attribute of the above model without doing another query.

    
asked by anonymous 22.06.2016 / 16:21

1 answer

3

Laravel saves the original attributes of the model within the original property.

$user = User::first();

$user->name = 'Rafael';

dd(
    $user->getAttributes(), // contém o atributo nome novo.
    $user->getOriginal() // contém o atributo nome antigo.
);

If you want to access a specific attribute of the original values, simply pass its name by parameter of Model::getOriginal .

Example:

  $user->getOriginal('name');

You can check directly in the api, in this link .

    
22.06.2016 / 16:40