Access to the original attribute value of the masculine attribute in Laravel 5

Say I have a Foo model and I mutate the getter attribute, for example:

 class Foo extends Model { protected $table = 'foo'; public function getSomeBarAttribute($value) { return some_function($value); } } 

Is there a way to access the original attribute value, pre-mutation?

+5
source share
3 answers

The method you are looking for is getOriginal . To get the original value you can use:

 $this->getOriginal('some_bar'); 
+13
source

The model defines this var:

 /** * The model attribute original state. * * @var array */ protected $original = array(); 

It is protected, so you must add a function to get the initial values.

(not verified)

+1
source
 $currentSomeBar = $foo->some_bar $oldFoo = new Foo($foo->getOriginal()); $oldSomeBar = $oldFoo->some_bar; 
0
source

All Articles