简体   繁体   中英

How can I search encrypted data in Laravel using like operator

How can I search data in Laravel using like operator, I have used

encrypt($searchValue); 

OR

Crypt::encryptString($searchValue)

But both return full encrypted data but I need searchable data using like operator, In that case, the first name is the encrypted format when is search normal text it returns null


User::where('first_name', 'like', '%' . 'abc' . '%')->get();
//it's return null

When I user

//searchValue is like only 'ab'
User::where('first_name', 'like', '%' . Crypt::encryptString($searchValue) . '%')->get();
//it's also return null 

Crypt::encryptString('abc') will output a slightly different string each time you call it. For that reason, you can't do something like:

User::where('first_name', 'LIKE', '%abc%')->get();
# OR
User::where('first_name', 'LIKE', '%' . Crypt::encryptString('abc') . '%')->get();

If you're encrypting the string and saving it to the database, the point is that you can't see it as plain-text, and by extension, can't search against it.

Sidenote, your first case %abc% might return a result, if another encrypted string has that sequence, but it will not be a reliable way to search for the un-encrypted value of abc , so you can't rely on that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM