Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mostafa74's avatar

modify data before store with PHP Traits

I try to Encrypt some Keys in Laravel's model with PHP trait and I code this :

<?PHP


namespace App\EncryptorTraits;


use blackpanda\encryptor\Encryptor;
use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Contracts\Encryption\EncryptException;
use Illuminate\Encryption\Encrypter;

trait Encryptable
{

    public function setAttribute($key,$val)
    {
        if($this->shouldEncrypt($key) && !$this->isEncrypted($val))
        {
            $val = $this->encryptAttribute($val);
        }

        return parent::setAttribute($key,$val);
    }

    public function getAttributeFromArray($key)
    {
        return $this->doDecryptAttribute($key,parent::getAttributeFromArray($key));
    }

    public function getArrayableAttributes()
    {
        return $this->doDecryptAttributes(parent::getArrayableAttributes());
    }

    public function getAttributes()
    {
        return $this->doDecryptAttributes(parent::getAttributes());
    }





    public function doEncryptAttributes($key)
    {
        if($this->shouldEncrypt($key) && !$this->isEncrypted($this->attributes[$key])){
            $this->attributes[$key] = $this->encryptAttribute($this->attributes[$key]);
        }
    }

    public function encryptAttribute($value)
    {

        try {
            $encrypted = $this->getEncrypter()->encrypt($value);
        } catch (EncryptException $e) {
            throw new EncryptException($e->getMessage(), $e->getCode());
        }

        return $this->getEncryptionPrefix() . $encrypted;
    }


    public function decryptAttribute($value)
    {
        if( !$this->isEncrypted($value) ) return $value;

        try{
            $decrypted = $this->getEncrypter()->decrypt(str_replace($this->getEncryptionPrefix(), '', $value));
        }
        catch (DecryptException $e)
        {
            throw new DecryptException($e->getMessage(),$e->getCode());
        }

        return $decrypted;
    }

    public function doDecryptAttribute($key , $val)
    {
        if($this->shouldEncrypt($key) && $this->isEncrypted($val))
        {
            return $this->decryptAttribute($val);
        }

        return $val;
    }

    public function doDecryptAttributes($attributes)
    {
        foreach ($attributes as $key => $val)
        {
            $attributes[$key] = $this->doDecryptAttribute($key,$val);
        }

        return $attributes;
    }





    protected function getEncryptionPrefix()
    {
        return config('encryptor.db_encryption_prefix');
    }

    protected function getEncryptableList()
    {
        return (isset($this->encryptable)) ? $this->encryptable : [];
    }

    protected function shouldEncrypt($key) : bool
    {
        $encryptableList = $this->getEncryptableList();

        return (in_array($key, $encryptableList));
    }

    protected function isEncrypted($value)
    {
        return strpos((string)$value, $this->getEncryptionPrefix()) === 0;
    }

    protected function getEncrypter()
    {
        return new Encrypter($this->getEncryptionSecret(),'AES-256-CBC');
    }

    protected function getEncryptionSecret()
    {
        $encryptor = new Encryptor();
        return $encryptor->getDatabaseSecret();
    }
}

This trait encrypts value if the key existed in Encryptable array in Model and set Attributes works but data stored in the database Unencrypted.

I try store data Like This

$Create = \App\myModel::create([
        'key1' => 'val1',
        'key2' => 'val2',
        ..
        ..
 ]);

and also I tried Save method but data still stored unencrypted in database!

and here is my model

<?php


namespace App;


use App\EncryptorTraits\Encryptable;
use Illuminate\Database\Eloquent\Model;

class myModel extends Model
{
    use Encryptable;

    protected $encryptable = ['name'];

    protected $guarded = ['id'];
    protected $table = 'table';

}

my problem is the Encryptable data didn't stored encrypted in database

0 likes
1 reply
fylzero's avatar

It looks like you don't actually call any of the functions of your trait in the class.

Try using the trait on your controller and calling the encryption method like this...

$Create = \App\myModel::create([
        'encryptedkey' => $this->encryptionMethodFromTrait('value'),
        'key2' => 'val2',
        ..
        ..
 ]);

23 likes

Please or to participate in this conversation.