Very helpful discussion!!! Thanks to @martinbean
I found in 5.2, when I implemented the new getAttribute() function as described above, when starting with a new model instance, getAttribute() struggled with an empty $value for a field in the $encryptable array. More specifically the the getJsonPayload() in BaseEncrypter.php threw a 'The payload is invalid' error.
Adding a test for an empty $value worked nicely, as seen below.
public function getAttribute($key)
{
$value = parent::getAttribute($key);
if (in_array($key, $this->encryptable)) {
$value = empty($value) ? null : Crypt::decrypt($value);
}
return $value;
}
public function setAttribute($key, $value)
{
if (in_array($key, $this->encryptable)) {
$value = empty($value) ? null : Crypt::encrypt($value);
}
return parent::setAttribute($key, $value);
}
In my application, these fields are signatures. An empty field with, 0 or '' value is encrypted, so setting the otherwise empty field to null is necessary to determine if the signature is missing.