Differences between Seed, migrations and factories?

3

I am learning Laravel, but it causes me conflict to understand some things about the subject.

As I understood the migrations are a kind of control of versions of the database with OOP

Seeds are used to create test data.

And the factories are supersets for the test data.

However, I do not understand the concepts very clearly, and I do not know very well how to apply them in Laravel, or an exercise.

    
asked by Cragser 23.05.2018 в 00:54
source

1 answer

2

Let's start the explanation

1. MIGRATIONS

Migrations are the intermediate layer between your app and the database manager; I explain, when you traditionally develop a software you start to create your relational model in tools such as MySQL's Work Bench or from the console you create each of the tables that will compose your database, however Laravel as a framework provides the ability to write each table in our database in the form of migration; so that in this way you can generate a version of it.

The structure of a migration is very similar to this one that I show you however it depends on what you are trying to solve

public function up()
    {
        Schema::create('flights', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('airline');
            $table->timestamps();
        });
    }
  

Each migration represents an entity / table in your database so   both the name that you place them must be representative of each one   of them and not placing more than one at a time

2. SEEDS

Seeds are a Laravel method that will allow you to populate your database with test information; therefore as notes to be able to use this feature of Laravel you must have your database created before (Clarification so that this quality works does not depend on you having made migrations), that is, you can perfectly create your database of all the life and later use this seed tool to also fill your data tables

To be able to start with the seeders, you must create them from the console as in the following example

php artisan make:seeder UsersTableSeeder

As you can see, the seeder is named with the name that the table will have but in the singular.

What then do the seeders have?

  

Sample data sample your database for testing purposes, it remains   Of course it should not be the production database but one of   test; will simplify this process because instead of being   filling in manually each table you can quietly generate   multiple and make tests

Likewise I mention the Seeders work with the query builder Fluent therefore you can see in the example that instead of inclosing the model, they work directly invoking the name of the table

Example

public function run()
    {
        DB::table('users')->insert([
            'name' => str_random(10),
            'email' => str_random(10).'@gmail.com',
            'password' => bcrypt('secret'),
        ]);
    }

3. MODEL FACTORIES

They are a tool to massively populate our database and even help us write tests for the information entered, likewise the Model Factories require to invoke the model that is linked to any of your tables in your database. data

Example

$factory->define(App\User::class, function ($faker) {
    return [
        'name' => $faker->name,
        'email' => $faker->email,
        'password' => str_random(10),
        'remember_token' => str_random(10),
    ];
});
    
answered by 23.05.2018 в 01:16