Skip to content

Instantly share code, notes, and snippets.

@mrl22
Last active April 19, 2022 09:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrl22/d91345c88c7f2519a49fb5ad445e8128 to your computer and use it in GitHub Desktop.
Save mrl22/d91345c88c7f2519a49fb5ad445e8128 to your computer and use it in GitHub Desktop.
Encrypt unencrypted passwords in Laravel migration with progress bar
<?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