Laravel Populated Factory


January 20th, 2020


Laravel Populated Factory provides an easy way to generate factory data for models based on types and database column names.

Using the make:populated-factory command by passing a model name and optional factory name override will create a new factory file based on the model. The command assumes the App namespace, so you must pass the full namespace for models located elsewhere:

1php artisan make:populated-factory User

The result is a populated factory file based on your model:

3use Faker\Generator as Faker;
5$factory->define(\App\User::class, function (Faker $faker) {
6 return [
7 'name' => $faker->name,
8 'email' => $faker->unique()->safeEmail,
9 'email_verified_at' => $faker->dateTime,
10 'password' => '$2y$10$uTDnsRa0h7wLppc8/vB9C.YqsrAZwhjCgLWjcmpbndTmyo1k5tbRC',
11 'remember_token' => $faker->sha1,
12 'created_at' => $faker->dateTime,
13 'updated_at' => $faker->dateTime,
14 ];

You can learn more about this package, get full installation instructions, and view the source code on GitHub at coderello/laravel-populated-factory.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.