Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

266
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda