How to return difference after running update in Laravel 4?


Does anyone know how to return only the fields and values changed in an update in laravel?

ex: a table that contains a field name with value John, let's assume that I change this field to John. I would like to know how to return the changed field after the update.

asked by anonymous 24.04.2016 / 14:38

1 answer


For you to do this you can do a "little maneuver" using the getDirty method.

This method is used to bring in the attributes that were changed before the model was updated.

So you could do it like this:

   $usuario = Usuario::find($id);


   $campos_alterados = $usuario->getDirty();


I think this is the simplest example. But we can also consider using some PHP OOP features.

For example, we can clone the object before changing it and comparing the differences:

 $usuario = Usuario::find($id);

 $clone = clone $usuario;


 array_diff($clone->getAttributes(), $usuario->getAttributes());
25.04.2016 / 15:02