I have Foo
Attribute that use getFooAttribute
method to format it before display but in some places, I need original attribute for it. So how can I do it?
Getting the original value of a particular attribute from v4.2 onwards:
$originalFoo = $model->getOriginal('foo');
If you want to use mutator in the majority of the code but sometimes would like to access the original value, you can do it by fetching all attributes using getAttributes() method of your model and then fetching the value from there, e.g.:
$originalFoo = $model->getAttributes()['foo'];
If you want the raw field straight out of database skipping the mutator then
$mode->getRawOriginal('attribute')
is the way to go. $model->getOriginal()
will give you the mutator value not the raw data you want.
see https://laravel.com/api/7.x/Illuminate/Database/Eloquent/Concerns/HasAttributes.html#method_getRawOriginal
I am using 5.3 and for this I use $model->getOriginal()['foo']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With