Last active
April 19, 2022 09:27
-
-
Save mrl22/d91345c88c7f2519a49fb5ad445e8128 to your computer and use it in GitHub Desktop.
Encrypt unencrypted passwords in Laravel migration with progress bar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use App\Models\Company; | |
use Illuminate\Database\Migrations\Migration; | |
use Symfony\Component\Console\Helper\ProgressBar; | |
use Symfony\Component\Console\Output\ConsoleOutput; | |
class EncryptPasswords extends Migration | |
{ | |
/** | |
* Run the migrations. | |
* | |
* @return void | |
*/ | |
public function up() | |
{ | |
$companies = Company::where('password', '')->get(); | |
$output = new ConsoleOutput(); | |
$progress = new ProgressBar($output, count($companies)); | |
$progress->start(); | |
foreach ($companies as $company) { | |
$company->password = Hash::make($company->comp_password); | |
$company->save(); | |
$progress->advance(); | |
} | |
$progress->finish(); | |
$output->write('', true); | |
} | |
/** | |
* Reverse the migrations. | |
* | |
* @return void | |
*/ | |
public function down() | |
{ | |
Company::where('password', '!=', '')->update([ | |
'password' => '' | |
]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment