Search code examples
laraveleloquentlumen

How to optionally call mutator in lumen


i use mutator in my model to encrypt id:

public function getIdAttribute($value)
{
    return encrypt($value);
}

but I want the default value to be the original value of the id and call the mutator when needed. is that possible?


Solution

  • If you want to be able to call the original value, and sometimes the encrypted value why don't you just add an extra function to your model ? You won't use a mutator since you want to be able to grab the original value, but you can add an extra function like this in your model which you will be able to call when you want to receive encrypted value.

    public function encryptedId()
        {
            return encrypt($this->id);
        }
    

    Or am I missing something?