Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get Original Attribute for Eloquent Model Laravel 5.1

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?

like image 818
Mohamed Gamal Avatar asked Dec 09 '15 14:12

Mohamed Gamal


4 Answers

Getting the original value of a particular attribute from v4.2 onwards:

$originalFoo = $model->getOriginal('foo');

like image 144
Bower Avatar answered Nov 19 '22 19:11

Bower


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'];
like image 19
jedrzej.kurylo Avatar answered Nov 19 '22 21:11

jedrzej.kurylo


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

like image 13
Ali A. Dhillon Avatar answered Nov 19 '22 21:11

Ali A. Dhillon


I am using 5.3 and for this I use $model->getOriginal()['foo']

like image 6
Dewan159 Avatar answered Nov 19 '22 19:11

Dewan159