How to return difference after running update on Laravel 4?

Asked

Viewed 183 times

1

Can anyone tell me how to return only the changed fields and values in an update to the Standard?

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

1 answer

1


To do this you can make a "small maneuver" using the method getDirty.

This method serves to bring the attributes that were changed before the model update.

So you could do like this:

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

   $usuario->fill(Input::all());

   $campos_alterados = $usuario->getDirty();

   $usuario->save();

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

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

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

 $clone = clone $usuario;

 $usuario->update($inputs);

 array_diff($clone->getAttributes(), $usuario->getAttributes());

Browser other questions tagged

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