Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the static password work in the default Laravel user factory?

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.

like image 994
DatsunBing Avatar asked May 18 '17 03:05

DatsunBing


People also ask

How does laravel store passwords?

The Laravel Hash facade provides secure Bcrypt and Argon2 hashing for storing user passwords. If you are using one of the Laravel application starter kits, Bcrypt will be used for registration and authentication by default.

What does factory do in laravel?

As you can see, in their most basic form, factories are classes that extend Laravel's base factory class and define a definition method. The definition method returns the default set of attribute values that should be applied when creating a model using the factory.

How do you make a factory in laravel?

For Laravel 8 With the artisan command, you no longer need to specify the model. By automatically including the factory trait in the model, Laravel automatically makes the factory methods available to the corresponding model. It is then sufficient to call the model in the seeder or in the tests.

What is the use of factory in laravel 8?

Using factory with faker library to seed fake data into database table. This tutorial will be super easy to understand and easy to implement in your code. This tutorial will guide you to understand How to use factory in seeder laravel 8 tutorial. Factory is the feature of laravel artisan.


1 Answers

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

See the comment at the bottom

bcrypt() is an expensive call, which is part of its advantage as a password hashing algorithm. Since the fake password being generated is hardcoded to 'secret', there is no need to bcrypt() the password every time. By using a static variable, we can bcrypt() the password once, and then use that same hash value on every subsequent call to the factory (within the same request). So, for example, imagine you were setting up a test that needed 100 users. $users = factory(User::class, 100)->create(); That code will call the factory closure 100 times. Without the static $password variable, bcrypt() would run 100 times, which could take a couple seconds. With the static variable, bcrypt() now only runs once, and the result is used for all 100 users, which greatly increases the speed of your test.

So the static $password is not for including some password from the outside. It is just a clever trick to gain performance.

like image 174
pr4xx Avatar answered Sep 20 '22 07:09

pr4xx