Difference Between Factory And Seeders In Laravel
Today, we will learn the Difference Between Factory And Seeders In Laravel. Most Laravel developers get confused about the difference between Factory and Seeders because Factory and Seeders look similar to use. Even developers don’t know the actual use. So in this tutorial, I will give you an example of the Difference Between Factory And Seeders.
Why We Use Factory and Seeders in Laravel?
Factory and Seeders both are used to generate Fake or Test data. When you use Factory and Seeders, then you don’t need to add manually data to your database.
SEEDERS
What are Seeders?
Seeder is a Class. Seeder is used to create Fake or Test data by using table name.
How to Create Seeders in Laravel?
We run an Artisan command make:seeder
, So Let’s start step by step.
Step 1: Run an Artisan command
php artisan make:seeder UsersSeeder
Step 2: Open the database/seeds/UsersSeeder.php file
<?php
#database/seeds/UsersSeeder.php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB; //Added
use Illuminate\Support\Facades\Hash; //Added
use Illuminate\Support\Str; //Added
use App\Models\User; //Added
class UsersSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
// Using Modal
$users = [
[
'name' => Str::random(8),
'email' => Str::random(9).'@gmail.com',
'password' => Hash::make('password')
],
[
'name' => Str::random(8),
'email' => Str::random(9).'@gmail.com',
'password' => Hash::make('password')
]
];
foreach ($users as $key => $value) {
User::create($value);
}
// Using DB
DB::table('users')->insert(
[
[
'name' => Str::random(10),
'email' => Str::random(10).'@gmail.com',
'password' => Hash::make('password'),
],
[
'name' => Str::random(10),
'email' => Str::random(10).'@gmail.com',
'password' => Hash::make('password'),
],
]
);
}
}
Now open the database/seeds/DatabaseSeeder.php
file and add UsersSeeder.
<?php
#database/seeds/DatabaseSeeder.php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
$this->call(UsersSeeder::class); //Added
}
}
Then go to the CLI and run the below command.
php artisan db:seed
Output
Seeding: Database\Seeders\UsersSeeder
Seeded: Database\Seeders\UsersSeeder (456.25ms)
Database seeding completed successfully.
FACTORY
What is Factory?
The factory is a helper function. The Factory is used to create Fake or Test data by using the Model. A factory is also a global object. The factory uses a Faker class to generate dummy data. Factory can also generate relationship data with the Model which the seeder can’t do.
How to Create Factories?
We run an Artisan command make:factory
, So Let’s start step by step.
Step 1: Run an Artisan command
php artisan make:factory PostsFactory
Step 2: Open the database/factories/PostsFactory.php file
<?php
#database/factories/PostsFactory.php
namespace Database\Factories;
use App\Models\Posts;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
class PostsFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Posts::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'title' => $this->faker->title,
'description' => $this->faker->text
];
}
}
Now open the database/seeds/DatabaseSeeder.php
file.
<?php
#database/seeds/DatabaseSeeder.php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
\App\Models\Posts::factory(10)->create(); //Added
}
}
Then go to the CLI and run the below command.
php artisan db:seed
Database seeding completed successfully.
When Should I Use Seeders Or Factory?
It is not meant the seeders or factories are not useful. When we have a predefined dataset add in data like a large number of posts, users, etc.
when application testing, we need to add dummy data. Create default users like Administrators, Customers, etc.