Laravel - Update all fields from a large table

Asked

Viewed 1,415 times

2

I have the following problem...

I am migrating a system, and in the same I own a client table

This table has a field called "cli_password", in which the customer’s password

To use Laravel authentication I had to create a column "password"

Now I need to replicate all the passwords in the column "cli_password" for the column "password".

But I can’t just duplicate the column cli_password and rename to password

Or perform a simple SQL to update the column password taking the value of cli_password for the field cli_password has no encryption.

Then I am obliged to go through all the fields of the table by performing the proper Hash.

But the table has more than 40 thousand records, and every time I query the screen returns blank, I can only select 5 thousand records through the "Chunk"

Would anyone have any suggestions or methods that are more efficient than doing multiple searches and updating their records?

2 answers

4


Hello,

the code you will make is very simple.

$users = User::wherePassword(null)->take(5000);
foreach($users as $user){
    $user->password = Hash::make($user->cli_senha);
    $user->save();
}
  • Thank you so much for the answer, that’s right, I’m updating all the records picking from 5000 to 5000, I could also increase the connection time of Mysql (not to close after 30 seconds)

  • It may be, but with this script it doesn’t hurt you to lose the connection by timeout. It will only get the records that are with null password. Updated, a hug.. Puts it in a url and runs when you give hahaha. Is mysql timeout? If php is set_time_limit(0)

1

can create an Migration to rename the column and update passwords:

Schema::table('users', function($table) {
    $table->renameColumn('cli_senha', 'password');
});

foreach(User::all() as $user) {
    $user->password = Hash::make($user->password);
    $user->save();
}

http://laravel.com/docs/schema#renaming-Columns

  • Miguel, renaming the column only, will not solve his problem, as the passwords are without HASH of the Laravel. :)

  • 1

    thx. Updated response

Browser other questions tagged

You are not signed in. Login or sign up in order to post.