Laravel Seed and Migrate data

To start with this we should first have a configuration for or database

First of all we'll need to install the migration table before anything, just run this command on our project path:

php artisan migrate:install

Then we´ll need to create a migration file for our tables in this example we are going to create a file for the default users model, running:

php artisan migrate:make create_users_table --table=users --create

Now we should have in app\database\migrations a file called XXXX_XX_XX_XXXXXX_create_users_table.php on that file, that file will containt our table user fields and that from where Laravel is gonna take the code to create the tables into our database.

Once we have our tables in our database we could populate that tables with data to acommplishe that we have to:

  • Uncomment from DatabaseSeederthe line 14
  • And create the file app\database\seeds\UserTableSeeder.php

Where we are going to populate our table, like:

Inside of the run method

User::create([
    'email' => $faker->email,
    'username' => $faker->userName,
    'password' => $faker->word,
    'password_temp' => $faker->word,
    'remember_token' => str_random(60),
    'code' => str_random(60),
    'active' => $faker->boolean(50)
]);