Ask Your Question
4

In Laravel library, what is the method to search encrypted data in MySQL using the LIKE %% operator?

asked 2023-04-03 11:00:00 +0000

nofretete gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-03-10 01:00:00 +0000

lalupa gravatar image

In Laravel library, to search encrypted data in MySQL using the LIKE %% operator, we can use the following steps: 1. First, we need to retrieve the encrypted data from the database using the selectRaw method in the query builder. 2. Then, we can use the whereRaw method to apply the LIKE %% operator to the encrypted data. 3. Finally, we can retrieve the search results using the get method.

Example:

$searchTerm = 'keyword'; // the search term entered by user
$encryptedSearchTerm = encrypt($searchTerm); // encrypt the search term

$results = DB::table('mytable')
    ->selectRaw('*, AES_DECRYPT(fieldname, "mysecretkey") as decrypted_fieldname')
    ->whereRaw('AES_DECRYPT(fieldname, "mysecretkey") LIKE ?', ["%{$encryptedSearchTerm}%"])
    ->get();

Note: In this example, we assume that the fieldname column in the mytable table contains the encrypted data, and the encryption key is "mysecretkey".

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-04-03 11:00:00 +0000

Seen: 8 times

Last updated: Mar 10 '23