Hello, Mr. asdfgh qweefg! Welcome to kgjhjgjh!

Tag: Laravel 5.0+, Faker

Hello, Mr. asdfgh qweefg! Welcome to kgjhjgjh!

Thank you. Only the name's not "asdfgh qweefg".

Sorry! It's just easier..

You're always showing everyone on Laravel Quick Tips these helpful little tricks. Why not show them how to give me a proper name?

Nice plug! And good idea.

//commandline `php artisan make:seeder UserTableSeeder` // UserTableSeeder.php use Illuminate\Database\Seeder; use App\User; class UserTableSeeder extends Seeder { public function run() { User::truncate(); $faker = Faker\Factory::create(); User::create([ 'name' => $faker->name, 'email' => $faker->email, 'age' => $faker->numberBetween(1, 100), 'city' => $faker->city, ]); } }

Hello, Mr. Earnest Wunsch! Welcome to...

Name's Davin Tremblay

But I just made...oh!

... $faker->seed(123); ...

Very nice. Except I'm Danish...

$faker = Faker\Factory::create('da_DK'); ...

Hans Egholm Erichsen, aged 58, from Ålbæk?

Well, I'm from Ålbæk, but I live in Spain...

$faker = Faker\Factory::create('da_DK'); $faker->seed(123); $faker_sp = Faker\Factory::create('es_ES'); $faker_sp->seed(123); User::create([ 'name' => $faker->name, 'email' => $faker->email, 'age' => $faker->numberBetween(1, 100), 'city' => $faker_sp->city, ]);

Lovely! Now...if I just had a few friends...

for ($i = 0; $i < 500; $i++) { User::create([ 'name' => $faker->name, 'email' => $faker->email, 'age' => $faker->numberBetween(1, 100), 'city' => $faker_sp->city, ]); }

Not sure I need that many friends. But more important, why so many babies and Old Farts? Can't you get more of them closer to my age?

... 'age' => $faker->optional($weight = 0.4, $default = 58)->numberBetween(1, 100),

40% of us are 58 years old? You might have tried a standard deviation.

I have to leave something for the readers or they'll feel spoon fed. Why are you using "Gunnar.Stokholm@hotmail.com" for an email?

Because you left that for the readers...?

for ($i = 0; $i < 500; $i++) { $name = $faker->name; // add a random number when using so many values, to ensure random emails $email = str_replace(' ', '.', (strtolower($name))) . $faker->numberBetween(1, 100) . '.mail.dk'; User::create([ 'name' => $name, 'email' => $email, 'age' => $faker->optional($weight = 0.4, $default = 58)->numberBetween(1, 100), 'city' => $faker_sp->city, ]); }

That's pretty good! You want my driver's license, as well? 25AW-657-QAZ

... 'license' => strtoupper($faker->bothify('##??-###-???')),

Great! Now if you just add our dogs, we'll be all set.

Dogs? What kind of dogs?

Poodles, collies and Dobermans. We don't all have them, but no one has more than three.

// factories/ModelFactory.php ... $factory->define(App\Dogs::class, function (Faker\Generator $faker) { return [ 'name' => $faker->firstName, 'age' => $faker->randomDigit, 'breed' => $faker->randomElement(['Boxer', 'Border Collie', 'Doberman Pinscher']), ]; }); // UserTableSeeder.php use App\User; use App\Dogs; ... User::truncate(); Dogs::truncate(); ... $user = User::create([ 'name' => $name, 'email' => $email, 'age' => $faker->optional($weight = 0.4, $default = 58)->numberBetween(1, 100), 'city' => $faker_sp->city, 'license' => strtoupper($faker->bothify('##??-###-???')), ]); foreach (range(0, rand(0, 3)) as $value) { $user->dogs()->save(factory(App\Dogs::class)->make()); }

That's nifty. The `dogs` table has a user_id field and relationships, so it gets automatically populated this way? Why didn't you use the default User factory model?

I wanted to show the simpler way first. It would be better to use the factory so we could recreate them for all our tests without duplicating code.

What if I wanted all our dogs' names to be Klingon?

Make your own Provider for Klingon language like this: Faker Internals: Understanding Providers. Be sure to submit it as a PR afterwards, to build up the data library.

Great! So...why were you calling me "Mr. asdfgh qweefg" at the beginning of this?

It was...easier...

Mmmmm. How are your tests?

Author's Note: I used Laravel's Seeding & Factory Model code, but if you are using something else to build your app, Phinx is wonderful and has Seeders, and of course Factory Muffin will do your Factory Model work for you.

Please let me know if any of the code was confusing because of the style I chose to write this in.

Hope that helps!