diff --git a/src/Illuminate/Database/Eloquent/Model.php b/src/Illuminate/Database/Eloquent/Model.php index f1d0564aaeed..89608957e12c 100644 --- a/src/Illuminate/Database/Eloquent/Model.php +++ b/src/Illuminate/Database/Eloquent/Model.php @@ -2793,6 +2793,17 @@ protected function isEncryptCastable($key) return $this->hasCast($key, ['encrypted']); } + /** + * Determine whether a value should be encrypted. + * + * @param string $key + * @return bool + */ + protected function isBase64Castable($key) + { + return $this->hasCast($key, ['base64']); + } + /** * Determine whether a value is JSON castable for inbound manipulation. * @@ -2850,6 +2861,8 @@ protected function castAttribute($key, $value) return new BaseCollection($this->fromJson($value)); case 'encrypt': return decrypt($value); + case 'base64': + return base64_decode($value); case 'date': case 'datetime': return $this->asDateTime($value); @@ -2889,6 +2902,10 @@ public function setAttribute($key, $value) $value = $this->asEncrypted($value); } + if ($this->isBase64Castable($key) && ! is_null($value)) { + $value = $this->asBase64($value); + } + if ($this->isJsonCastable($key) && ! is_null($value)) { $value = $this->asJson($value); } @@ -3063,6 +3080,17 @@ protected function asEncrypted($value) return encrypt($value); } + /** + * Encode the given value. + * + * @param mixed $value + * @return string + */ + protected function asBase64($value) + { + return base64_encode($value); + } + /** * Encode the given value as JSON. *