为 laravel 中的用户创建新密码

Create a new password for user in laravel

如何使用 str_random 创建新密码并保存在用户 table 密码中? 示例:

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

您可以使用 faker 生成假数据或虚拟数据,在 /database/seeds 上的项目文件夹中找到 DatabaseSeeder class。

然后添加这一行,如果你使用 Laravel 版本 7,你已经有一个工厂来创建假用户,你可以在 /database/factories 中检查你应该有 UserFactory.php 文件.

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

现在使用终端对 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();