@stueynet You would probably have the encryption at the database level, as if someone gets access to the database you don’t want them to be able to read people’s medical data in plain text.
You could create a trait that encrypts and decrypts data on save and retrieval respectively:
trait Encryptable
{
public function getAttribute($key)
{
$value = parent::getAttribute($key);
if (in_array($key, $this->encryptable)) {
$value = Crypt::decrypt($value);
}
}
public function setAttribute($key, $value)
{
if (in_array($key, $this->encryptable)) {
$value = Crypt::encrypt($value);
}
return parent::setAttribute($key, $value);
}
}
You can then just apply the trait to your models, and define a property called $encryptable that’s an array of columns whose data should be encrypted:
class Patient extends Model
{
use Encryptable;
protected $encryptable = [
'blood_type',
'medical_conditions',
'allergies',
'emergency_contact_id',
];
}