Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access raw Eloquent mutated attribute value in Laravel 5

Say I have a model Foo and I'm mutating an attribute getter, like so:

class Foo extends Model
{   
    protected $table = 'foo';

    public function getSomeBarAttribute($value)
    {
        return some_function($value);
    }
}

Is there a way to access the attribute's raw value, pre-mutation?

like image 388
Ahmed Nuaman Avatar asked Aug 21 '15 13:08

Ahmed Nuaman


2 Answers

The method you look for is getOriginal. To get the original value you might use:

$this->getOriginal('some_bar');
like image 169
Marcin Nabiałek Avatar answered Nov 09 '22 10:11

Marcin Nabiałek


In model is defined this var:

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

It's protected so you should add a function to get original's values

(not tested)

like image 1
maztch Avatar answered Nov 09 '22 08:11

maztch