简体   繁体   English

为 laravel 中的用户创建新密码

[英]Create a new password for user in laravel

how create new password with str_random and save in users table password?如何使用 str_random 创建新密码并将密码保存在用户表中? example:例子:

$newPassword = str_random(8);
    $userFind = User::all()->where('numberPhone', $request->phone);
    $userFind->password = hash::make($newPassword);

You can user faker to generate fake or dummy-data, find the DatabaseSeeder class inside your project folder on /database/seeds .您可以使用 faker 生成假数据或虚拟数据,在/database/seeds上的项目文件夹中找到 DatabaseSeeder class。

Then add this line if you're using Laravel version 7, you have already a factory to create fake users, you can check for that inside the /database/factories you should have UserFactory.php file.如果您使用的是 Laravel 版本 7,则添加此行,您已经有一个工厂来创建假用户,您可以在/database/factories中检查您应该有 UserFactory.php 文件。

factory(App\User::class, 50)->create();

Now execute this command using the terminal to run the DatabaseSeeder:现在使用终端执行此命令以运行 DatabaseSeeder:

php artisan db:seed
$newPassword = str_random(8);
$userFind = User::where('numberPhone', $request->phone)->first(); // assuming phone number is unique - get only one with first()
$userFind->password = Hash::make($newPassword); // you may need to check whether user exists before updating.
$userFind->save();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM