Skip to content

Instantly share code, notes, and snippets.

@sneycampos
Created April 9, 2021 19:31
Show Gist options
  • Save sneycampos/1bc1b750cd5017d1dcb98a429ba6d650 to your computer and use it in GitHub Desktop.
Save sneycampos/1bc1b750cd5017d1dcb98a429ba6d650 to your computer and use it in GitHub Desktop.
trait Encryptable laravel
<?php
use Illuminate\Support\Facades\Crypt;
trait Encryptable
{
/**
* Decrypt the column value if it is in the encrypted array.
*
* @param $key
*
* @return mixed
*/
public function getAttributeValue($key)
{
$value = parent::getAttributeValue($key);
if (in_array($key, $this->encrypted ?? [])) {
$value = $this->decryptValue($value);
}
return $value;
}
/**
* Decrypts a value only if it is not null and not empty.
*
* @param $value
*
* @return mixed
*/
protected function decryptValue($value)
{
if ($value !== null && !empty($value)) {
return Crypt::decrypt($value);
}
return $value;
}
/**
* Set the value, encrypting it if it is in the encrypted array.
*
* @param $key
* @param $value
*
* @return
*/
public function setAttribute($key, $value)
{
if ($value !== null && in_array($key, $this->encrypted ?? [])) {
$value = Crypt::encrypt($value);
}
return parent::setAttribute($key, $value);
}
/**
* Retrieves all values and decrypts them if needed.
*
* @return mixed
*/
public function attributesToArray()
{
$attributes = parent::attributesToArray();
foreach ($this->encrypted ?? [] as $key) {
if (isset($attributes[$key])) {
$attributes[$key] = $this->decryptValue($attributes[$key]);
}
}
return $attributes;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment