Skip to content
Advertisement

How to encrypt a column value on an already filled table with unencrypted data in an existing laravel projects?

I have a laravel project that already been running in production but now I want to encrypt confidential data which is salary column, here is the example of my table

|id|username|email|salary|
|1|xxx|xxx@gmail.com|1000|
|2|yyy|yyy@gmail.com|2000|

I want to use laravel encryption , so how to update my existing table and continue using laravel encryption

Advertisement

Answer

Instead of running any query into the database I’d prefer to execute this:

$users = AppUser::all();

foreach ($users as $user) {
    $user->salary = encrypt($user->salary)
    $user->save();
}

You’ll need to add a migration to the project in order to change the salary type field to text

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement