Summer Sale! All accounts are 50% off this week.

crnkovic's avatar

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

If somebody could answer my title that would be great. Laravel documentation says nothing about it.

0 likes
4 replies
toniperic's avatar
Level 30

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.

11 likes
aurelianspodarec's avatar

What if I don't want random data, but insert a specific set of array. Say for instance, I have categories and I put the name in an array, and then I want the exact names to be populated in the database, how would you go about doing that?

So

$platforms = [
            'web',
            'application',
            'mobile'
        ];\

How would you put that in the database?

aurelianspodarec's avatar

@Sergiu17 Ty, not what I was looking for, but figured it out

public function createIndustry(string $industry)
    {
        Industry::factory()->create([
            'name' => $industry,
            'slug' => $industry,
        ]);
    }
     
    public function run()
    {
        $industries = ['hotel', 'sport', 'travel', 'automobile'];
    
        collect($industries)->each(fn (string $industry) => 
            $this->createIndustry($industry)
        );
    }

Please or to participate in this conversation.