Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -2793,6 +2793,17 @@ protected function isJsonCastable($key)
return $this->hasCast($key, ['array', 'json', 'object', 'collection']);
}

/**
* Determine whether a value should be encrypted.
*
* @param string $key
* @return bool
*/
protected function isEncryptCastable($key)
{
return $this->hasCast($key, ['encrypt']);
}

/**
* Get the type of cast for a model attribute.
*
Expand Down Expand Up @@ -2842,6 +2853,8 @@ protected function castAttribute($key, $value)
return $this->asDateTime($value);
case 'timestamp':
return $this->asTimeStamp($value);
case 'encrypt':
return decrypt($value);
default:
return $value;
}
Expand Down Expand Up @@ -2872,6 +2885,10 @@ public function setAttribute($key, $value)
$value = $this->fromDateTime($value);
}

if ($this->isEncryptCastable($key) && ! is_null($value)) {
$value = $this->asEncrypted($value);
}

if ($this->isJsonCastable($key) && ! is_null($value)) {
$value = $this->asJson($value);
}
Expand Down Expand Up @@ -3058,6 +3075,17 @@ public function fromJson($value, $asObject = false)
return json_decode($value, ! $asObject);
}

/**
* Encrypt to given value.
*
* @param mixed $value
* @return string
*/
protected function asEncrypted($value)
{
return encrypt($value);
}

/**
* Clone the model into a new, non-existing instance.
*
Expand Down