Per this link, https://laravel.com/docs/5.4/database-testing#writing-factories, the default Laravel user factory tests the value of a static $password variable. If it is falsey, it bcrypts 'secret' and uses that.

How does one go about setting the value of the static variable $password? Obviously I don't want to import it at the time the function is declared (since that would defeat the purpose of making it variable). I realise that I can override the value of password by passing an array to the make() method, but this is a different thing altogether.

推荐答案

I had the exact same question and found the answer here:

See the comment at the bottom

bcrypt()调用开销很大,这是其作为密码散列算法的部分优势. 由于生成的假密码被硬编码为"机密",因此不需要每次都对密码进行bcrypt(). 通过使用静电变量,我们可以对密码进行一次bcrypt()加密,然后在随后的每次工厂调用中使用相同的散列值(在相同的请求中). 例如,假设您正在设置一个需要$users = factory(User::class, 100)->create();个用户的测试. $users = factory(User::class, 100)->create();个 该代码将调用工厂关闭$users = factory(User::class, 100)->create();次.如果没有静电$PASSWORD变量,bcrypt()将运行$users = factory(User::class, 100)->create();次,这可能需要几秒钟的时间.使用静电变量,bcrypt()现在只运行一次,结果将用于所有$users = factory(User::class, 100)->create();个用户,这将极大地提高测试速度.

所以静电$密码不是用来包含外部的一些密码的.这只是一个获得性能的巧妙伎俩.

Laravel相关问答推荐

为什么Laravel在API请求时返回BadMethodCallException?

Laravel复选框值更新

在 blade laravel 中以格式编号显示长数字

使用正则表达式 laravel 忘记缓存

Laravel:BelongstoMany 关系实现

(1/1) ErrorException ReflectionFunction::__construct() ex

Laravel 没有定义

在 laravel 查询构建器中使用多个 where 子句

查询构建器中的 Laravel 5 多重 Select ()

PHP 中的 Http 响应代码枚举

Laravel 干预图像 GD 库扩展

使用限制排队 Guzzle 请求

从 Laravel Cashier 获取下一个账单日期

导入文件时如何跳过第一行

调用未定义的方法 Maatwebsite\Excel\Excel::load()

在 Laravel 中,如何获取 *only* POST 参数?

Laravel 5.3 通知 - 仅使用Electron邮件地址通知

如何判断 Laravel 集合是否为空?

Laravel or where

使用 Carbon 将小时转换为 PM 和 AM