How to use Laravel Seeders, Fakers, and Factories

  • What is the difference between Seeders, Fakers, and Factories?
  • Should I use them?
  • Do I need to use all of them or can I use some of them?
  • How do I create them?

Factories

php artisan tinker
User::factory()->make();
=> App\Models\User {#3477
name: "Dino Cajic",
email: "dinocajic@gmail.com",
email_verified_at: "2020-10-19 14:58:29",
}
User::factory()->create();

Fakers

php artisan tinker>>> User::factory()->make();
=> App\Models\User {#3477
name: "Mr. Milo Jast MD",
email: "herzog.mose@example.org",
email_verified_at: "2020-10-19 15:13:31",
}
>>> User::factory()->make();
=> App\Models\User {#3476
name: "Carolanne Hettinger",
email: "eladio62@example.org",
email_verified_at: "2020-10-19 15:13:34",
}
php artisan tinker>>> User::factory()->count(50)->make();=> Illuminate\Database\Eloquent\Collection {#3474
all: [
App\Models\User {#3479
name: "Geovanni Brakus",
email: "xokuneva@example.com",
email_verified_at: "2020-10-19 15:27:23",
},
App\Models\User {#3480
name: "Dr. Clara Nienow",
email: "jerde.orval@example.net",
email_verified_at: "2020-10-19 15:27:23",
},
App\Models\User {#3481
name: "Prof. Stephon Ziemann IV",
email: "sonia.mitchell@example.org",
email_verified_at: "2020-10-19 15:27:23",
},
App\Models\User {#3482
name: "Katrina O'Kon",
email: "jessika.gaylord@example.org",
email_verified_at: "2020-10-19 15:27:23",
},
App\Models\User {#3483
name: "Marlen Rosenbaum",
email: "hartmann.edwina@example.org",
email_verified_at: "2020-10-19 15:27:23",
},
...
php artisan tinker>>> User::factory()->count(50)->create();

Creating a Factory and Utilizing Faker

php artisan make:model Car -mf
Model created successfully.
Factory created successfully.
Created Migration: 2020_10_19_154353_create_cars_table
php artisan migrate
php artisan tinker>>> Car::factory()->count(3)->create();=> Illuminate\Database\Eloquent\Collection {#3474
all: [
App\Models\Car {#3478
make: "delectus",
model: "commodi",
updated_at: "2020-10-19 16:02:35",
created_at: "2020-10-19 16:02:35",
id: 1,
},
App\Models\Car {#3479
make: "illo",
model: "voluptates",
updated_at: "2020-10-19 16:02:35",
created_at: "2020-10-19 16:02:35",
id: 2,
},
App\Models\Car {#3480
make: "qui",
model: "nostrum",
updated_at: "2020-10-19 16:02:35",
created_at: "2020-10-19 16:02:35",
id: 3,
},
],
}

Creating a Factory after the Model has been created

php artisan make:factory CarFactory --model=Car

Seeders

// Can call multiple seeders
php artisan db:seed
php artisan db:seed --class=UserSeeder
php artisan make:seeder UserSeeder
php artisan db:seed --class=UserSeeder
php artisan db:seed --class=UserSeeder
php artisan make:seeder CarSeeder
php artisan db:seed --class=CarSeeder
php artisan db:seed
Seeding: Database\Seeders\UserSeeder
Seeded: Database\Seeders\UserSeeder (26.58ms)
Seeding: Database\Seeders\CarSeeder
Seeded: Database\Seeders\CarSeeder (14.14ms)
Database seeding completed successfully.
php artisan migrate:fresh --seed

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store