What’s a Seeder for?

Asked

Viewed 1,185 times

5

I saw in the Laravel that there is a folder called database. In it we have the Migrations and the Seeds.

I understand that Migrations are migrations, are codes that provide specifications for creating the tables in the database, without the need to know the SGDB that is intended to use.

But what would that be seeder or seed?

This Seeder name is some concept of programming, or something related to databases?

It is directly related to migrations (Migrations)?

Note: Remembering that the question is not about Laravel, but about the explanation about the name Seeder or Seed that appears there.

  • I don’t know if it has anything to do with it, but maybe How computer randomization is generated?.

  • @Guilhermelautert I think the name makes sense, but the purpose of the question does not. Seed is related to "sow" and Seeder "sower". On Laravel I saw that the Seeder serves to insert data in the database, but could not do this in Migration? So the confusion!

  • 1

    Seeder is the guy who has the full file on the machine :P >> torrents

2 answers

4


But what would this seeder or Seed?

It is nothing more than predetermined data that will be entered in the database at its initialization.

It is directly related to migrations (Migrations)?

Not with Migrations itself, but rather in the concept of "Code First", ie where you create your database according to the model you data that your application has.

And when should I use?

This concept is widely used in testing, but is not limited to testing. A good example is when you need to create a user whenever you are "installing" your system in a new environment. Instead of creating a user at hand, you can set up a Seed user and password that will be automatically entered into the system, simple no?
Another example would be the data in tables that will fill combobox, cities/states, among many other examples.

Sniffing a little focus...

You commented on larable, but I will put an example with the Entity Framework here.

public class SchoolDBInitializer : DropCreateDatabaseAlways<SchoolDBContext>
{
    protected override void Seed(SchoolDBContext context)
    {
        var user = new User{Name = "Admin", Password = "Admin"};
        context.Users.Add(user );
        base.Seed(context);
    }
}

In this example, every time the database is initialized by the code, the Admin user will be inserted into the database.

  • Despite being EF the example, gave to understand perfectly. I am training a lot C#, masters +1

2

Seed is the concept of "feeding" your application with test data. In Laravel, Seeders classes are responsible for entering test data into your application.

  • 2

    Gabriel, just to help, Seeders aren’t just for testing, they can be used in production as well.

  • Thanks Wallace for the addendum.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.