'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 1:[1]
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?
Solution 2:[2]
You can using getRawOriginal() to get original value in lumen:
for example:
$model = Model::find('model_id');
return $model->getRawOriginal('column_name'));
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Fabrice Lefloch |
Solution 2 | Mohammad Gholamrezaei |