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

matttonks11's avatar

Decrypting data in Eloquent Query

Hello there,

I have some data in my database that I would like to encrypt/decrypt when I make eloquent calls.

I added this encryptable trait from this discussion and it works fine when I use dd(Client::first()->mobile)

https://laracasts.com/discuss/channels/laravel/encrypting-model-data

However I'm trying to pass this data to some DataTables on the front-end using yajra/laravel-datatables package I'm currently passing the whole query, which means the front end is receiving the encrypted values not the decrypted values.

Does anyone know if it is possible to change my eloquent call to get the decrypted values?

Here's my code for passing the data to the front-end

$clientData = $client->with('groups:id,name')
            ->select('clients.id','clients.forename', 'clients.surname', 'clients.mobile', 'clients.email', 'clients.created_at')->latest();

return datatables()->of( $clientData )->toJson();

0 likes
12 replies
martinbean's avatar

@matttonks11 You could create a class that takes an Eloquent collection instance, spins through each model, and decrypts any encrypted values:

use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;

class ModelDecrypter
{
    public function decryptModel(Model $model)
    {
        foreach ($model->getEncryptable() as $attribute) {
            $model->setAttribute($attribute, decrypt($model->getAttribute($attribute)));
        }

        return $model;
    }

    public function decryptCollection(Collection $collection)
    {
        return $collection->map(function (Model $model) {
            return $this->decryptModel($model);
        });
    }
}
$clientData = $client->with('groups:id,name')
            ->select('clients.id','clients.forename', 'clients.surname', 'clients.mobile', 'clients.email', 'clients.created_at')->latest();

$clientData = (new ModelDecrypter)->decryptCollection($clientData);

return datatables()->of($clientData)->toJson();
matttonks11's avatar

@martinbean Hi thank you for this, it looks great. However i'm getting an error on the ->getEncryptable() method, it's saying it doesn't exist.

Call to undefined method App\Client::getEncryptable()

Here's my model...


class Client extends Model
{
    use Encryptable;

    protected $encryptable = [
        'email',
        'mobile'
    ];
}

And here's the trait class...

namespace App\Traits;
use Crypt;

trait Encryptable
{
    public function getAttribute($key)
    {
        $value = parent::getAttributeValue($key);

        if (in_array($key, $this->encryptable)) {
            return $value = Crypt::decrypt($value);
        }
        return $value;
    }

    public function setAttribute($key, $value)
    {
        if (in_array($key, $this->encryptable)) {
            $value = Crypt::encrypt($value);
        }

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

Not sure what I'm doing wrong, any advice?

1 like
D9705996's avatar

Do you need to add this to your trait

public function getEncryptable() {
  return $this->encryptable;
}
matttonks11's avatar

@D9705996 thank you, that's worked, now I'm getting an error that says the payload is invalid.

Any thoughts?

D9705996's avatar

If you dd(parent::getAttributeValue($key)); do you see the encrypted data?

matttonks11's avatar

I just get the value 2 returned, I'm guessing this is the number of encrypted fields?

marbobo's avatar

@D9705996 it seems that this package is old. do you have new package with the same features?

matttonks11's avatar

@D9705996 I think the error is something to do with this line maybe

//Model Decrypter.php

$model->setAttribute($attribute, decrypt($model->getAttribute($attribute)));

When I remove the decrypt helper function and run dd(parent::getAttributeValue($key)) i get the data back with encrypted values

In my Encryptable.php file I'm decrypting the data when the getAttribute method runs so I think its trying to decrypt the data twice maybe? Which is leading to the invalid payload error

//Encryptable.php

public function getAttribute($key)
    {
        $value = parent::getAttributeValue($key);

        //dd(parent::getAttributeValue($key));
        
        if (in_array($key, $this->encryptable)) {
            return $value = Crypt::decrypt($value);
        }
        return $value;
    }
D9705996's avatar

The source code looks very similar to your approach.

Please or to participate in this conversation.