megaman7's avatar

Unclear documentation

This is an issue regarding the documentation

this page https://laravel.com/docs/5.6/database-testing#generating-factories

has this code example (and a few similar ones)

" $factory->state(App\User::class, 'address', function ($faker) { return [ 'address' => $faker->address, ]; }); "

in all of these examples the line of code code "'address' =>$faker->address" has a field name from your DB/Model and a property (of $faker) by the same name.

This imples that to generate fake data you need to use the following code "$faker->some_property_name

What you actually have to do is provide a faker formatter as documented here https://github.com/fzaninotto/Faker#formatters

The laravel documentation does not make this clear, it does not even tell you that faker formatters exist.

0 likes
5 replies
Snapey's avatar

Are you sure?

The factory is injected with an instance of faker, and then you just grab the attributes;

From the github docs;

// generate data by accessing properties
echo $faker->name;
  // 'Lucy Cechtelar';
echo $faker->address;
  // "426 Jordy Lodge
  // Cartwrightshire, SC 88120-6700"

This is all I have done..

megaman7's avatar

name and address happen to be valid faker properties try replaceing one of them with lalalala and see what happens (even if lalalala is a valid DB/model column)

jlrdw's avatar

You stated

This is an issue regarding the documentation

If you feel there is a documentation error, let Taylor know with a pull request. This site is Jeffrey's.

Snapey's avatar

replaceing one of them with lalalala and see what happens

What? Faker won't know what lalalala is? Why should it? What has that got to do with the name of your columns?

Faker's job is to create random data - what you do with it is up to you. It does not need to go in a database.

Please or to participate in this conversation.