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

rajdipbacancy's avatar

Issue with encryption decryption in Laravel Query Builder

I am working on a new project that stores medical related information. We are working on a project that stored Healthcare related information. For Security reason we store users table fields value with encryption, to implement that we use trait that encrypts and decrypts on some selected fields of User Model and its working fine. Now I want to apply a filter on that encrypted data in Laravel query builder but the problem is that encrypted value seems to differ every time, even if given the same string.

So how can I apply a filter on database encrypted fields using Laravel query builder?

0 likes
8 replies
rajdipbacancy's avatar

Hello Kinomej,

Thanks for your reply.

I have a query like below in which I want to apply search on First name fields which data is encrypted into a database table

$query = User::select('users.id', 'first_name', 'last_name', 'address_line_1', 'cell_phone', 'specialities.name as primary_speciality')
            ->where('role_id', '3')
            ->leftJoin('provider_primary_specialities', 'users.id', '=', 'provider_primary_specialities.user_id', 'left')
            ->join('specialities', 'provider_primary_specialities.speciality_id', '=', 'specialities.id', 'left')
            ->where(function ($query) use ($data) {

                if (isset($data['search'])) {
                $query->orWhere('first_name', 'LIKE', '%' . $data['search'] . '%');
                }

                if (isset($data['search'])) {
                    $query->orWhere('address_line_1', 'LIKE', '%' . $data['search'] . '%');
                }

                if (isset($data['search'])) {
                    $query->orWhere('cell_phone', 'LIKE', '%' . $data['search'] . '%');
                }
                
            });

return $query->orderBy($data['orderColumn'] ?? 'users.created_at', $data['orderDirection'] ?? 'desc')
    ->paginate($data['per_page'] ?? 10);

How can I apply Laravel query builder filter on it?

devfrey's avatar
devfrey
Best Answer
Level 11

@RAJDIPBACANCY - For obvious reasons you can't use LIKE queries to search through encrypted data. Your database has no clue what the original values are. The only way you could 'search' your records is by loading them all into your application, decrypting them (one by one preferably) and doing a 'search' with PHP until you find your match.

rajdipbacancy's avatar

@devfrey : After doing lots of googling I found that I need to follow the same method which you suggest. Now for searching records, I am first loading all the data than decrypting it and apply a filter on it. this solution increases my result output time but I think there is no other proper solution rather than this.

Thanks for your reply.

packy's avatar

How did you end up doing this? I am trying to search encrypted data with Laravel Nova

Please or to participate in this conversation.