直接在 SQL 中手动创建 Laravel 哈希
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28408275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Manually creating laravel hash directly in SQL
提问by MakkyNZ
I have a site where user passwords are hashed using Laravel's Hash::make() function.
I now have a need to manually create user's directly in the DB (MySQL). Is there a way I can create the hashed password for the user using just raw SQL?
我有一个使用 Laravel 的 Hash::make() 函数对用户密码进行哈希处理的站点。
我现在需要直接在数据库(MySQL)中手动创建用户。有没有办法可以只使用原始 SQL 为用户创建散列密码?
采纳答案by Joel Hinz
Not really, I'm afraid. Laravel uses Bcrypt, which isn't available in MySQL. In addition, it's a bad practice to do it in raw SQL, because the passwords might end up in server query logs. Sorry. :/
不是真的,我害怕。Laravel 使用 Bcrypt,它在 MySQL 中不可用。此外,在原始 SQL 中执行此操作是一种不好的做法,因为密码可能最终会出现在服务器查询日志中。对不起。:/
回答by Panagiotis Koursaris
You can create the password that you want via php artisan tinker
:
您可以通过php artisan tinker
以下方式创建所需的密码:
- Open the terminal on the root of your project.
- Then write
php artisan tinker
. - Then
echo Hash::make('password')
;
- 打开项目根目录上的终端。
- 然后写
php artisan tinker
。 - 然后
echo Hash::make('password')
;
You will see the hashed password. You can do whatever you want with that password, even with sql query directly.
您将看到散列的密码。您可以使用该密码做任何您想做的事情,甚至可以直接使用 sql 查询。
回答by Jirennor
You could use the DB Seed option in Laravel and add your users that way. Via the DB seed you can use the hash method to hash the passwords.
您可以使用 Laravel 中的 DB Seed 选项并以这种方式添加您的用户。通过数据库种子,您可以使用散列方法对密码进行散列。
class UserTableSeeder extends Seeder {
public function run()
{
DB::table('users')->delete();
User::create(array(
'username' => 'user',
'password' => Hash::make('password')
));
}
}
But if can only use raw SQL I dont have a solution for you.
但是如果只能使用原始 SQL,我没有适合您的解决方案。
For more information about DB Seed see the following page. http://laravel.com/docs/4.2/migrations#database-seeding
有关 DB Seed 的更多信息,请参阅以下页面。 http://laravel.com/docs/4.2/migrations#database-seeding
回答by Surya
No!
不!
But you can write a function something like below
但是你可以写一个像下面这样的函数
<?php
// Extend the base controller etc..
public function insertUsers( $passwordArray ){
$passwordArray = array("user1" => "pass1", "user2" => "pass2");
foreach ($passArray as $user => $pass) {
$password = Hash::make($pass); // Laravel function
dbQuery("INSERT INTO users (`user`, `password`) VALUES ($user, $password);
}
}