Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

265
Views
What is the difference between Model Factory and a DB seeder in Laravel?

What is the difference between Model Factory and a DB seeder in Laravel?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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

over 4 years ago · Santiago Trujillo Report

0

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

over 4 years ago · Santiago Trujillo Report

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!