LARAVEL - Using bcrypt in a column with tinker

1

I know that using tinker I can create new records based on a model.

$user = new App\Datos;
>>> $user->name = 'jeff';
>>> $datos->email = '[email protected]';
>>> $datos->password = bcrypt('12345');

But! .. how would the bcrypt affect the entire password column. This is because the data I have from that column is not encrypted.

    
asked by DoubleM 06.12.2018 в 16:51
source

1 answer

0

You should do it this way:

$data = \DB::table("datos")->update(array("password" => bcrypt("password")));
  • That is, by using the facade DB we access the name of the table
  • later we access the update() method and pass it in the form of an associative array the column that we want to update and then the value that we want to generate, using the bcrypt() method to encrypt it
  • IMPORTANT NOTE

    bcrypt will generate a data of alphanumeric type so it is important that the column on which you are going to execute the update supports this, being able to be a VARCHAR otherwise you would get an error

    UPDATE

    If the update is not massive and you want to apply a where to indicate which values should be updated; you must do the following

    Here, for example, we tell you to only update if the id equals 2

    $data = \DB::table("datos")->where('id', 2)->update(array("password" => bcrypt("password")));
    

    If instead you want to run the update to a group of specific values but not all; then do the following

    $data = \DB::table("datos")
            ->whereIn('id', [1, 3])
            ->update(array("password" => bcrypt("password")));
    

    As notes I used the whereIn() method where the first argument is the name of the column and later in the form of a comma separated array the range of values that will be modified or updated

        
    answered by 06.12.2018 в 18:21