What is the difference between Model Factory and a DB seeder in Laravel?
3 Answers
I Prefer to view Seeders and Factories from another perspective.
As Mentioned by others, Model Factories are used for testing purposes and populating your database with a huge amount of fake data. This could be used for unit testing and performance testing of your system.
On the other hand, Seeder Classes are used for inserting data that is crucial and important for the system to operate with. Something like the superadminstrator, Basic Roles/Role permissions in the system, Countries, Cities and other data that may not come from CRUD's
-
1This is excellent answer, however immediately after I read it I asked myself, why ? whats the crucial difference ? For example why I shouldn't use seeder to populate my database with huge amounts of date for performance testing ? Commented May 21, 2021 at 9:12
-
3Good Question @JúliusĽuptovec, in seeder you can have some private data or confidential data but when you have to test your application you don't need confidential data instead you just need random and huge data so that's why it is good approach to have different files for both purpose– hu7syCommented May 26, 2021 at 8:10
-
5@JúliusĽuptovec Sure you CAN use Seeder as Factories. You CAN use Faker in Seeder. But I think its not how we SHOULD use seeder. . think seeder as prepopulate data for our Apps. suppose we have library apps, and we have default Predefined books category (e.g. fiction, medical, cooking, etc). In this case, the list for category we use Seeder. meanwhile factories as prepopulate data for our testing. So to test our apps, we need 100 'fake' library member, in here we use factories. . CMIIW Commented Jul 9, 2021 at 7:59
-
1@JúliusĽuptovec follow the link to get to actual difference devnote.in/difference-between-factory-and-seeders-in-laravel/…. Commented Feb 10, 2023 at 6:26
I have researched for your question and found something simple like below.
Factory & Seeder both are used for generating test data for the application.
Factory: By using factories you can easily create test data for your laravel application based on Model. In factory we are using another class like Faker to generate fake data easily.
In factory we can also generate data related to the relationship while in db seeder we can't.
factory(App\User::class, 50)->create()->each(function ($user) {
$user->posts()->save(factory(App\Post::class)->make());
});
Another example of factory:
use Illuminate\Support\Str;
use Faker\Generator as Faker;
$factory->define(App\User::class, function (Faker $faker) {
return [
'name' => $faker->name,
'email' => $faker->unique()->safeEmail,
'email_verified_at' => now(),
'password' => '$2y$10$TKh8H1.PfQx37YgCzwiKb.KjNyWgaHb9cbcoQgdIVFlYg7B77UdFm', // secret
'remember_token' => Str::random(10),
];
});
Seeder: By using seeder you can create test data based on your table name. Like below.
<?php
use Illuminate\Support\Str;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
DB::table('users')->insert([
'name' => Str::random(10),
'email' => Str::random(10).'@gmail.com',
'password' => bcrypt('secret'),
]);
}
}
You can check here for more information:
https://laravel.com/docs/5.7/seeding#using-model-factories
https://laravel.com/docs/5.7/database-testing#writing-factories
Database seeder is used to populate tables with data.
Model factories is a convenient centralized place to define how your models should be populated with fake data.
In seeder class you would leverage model factories, and model factories will most likely use another library to generate random fake data, such as fzaninotto/faker.