Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

264
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda