Migration files are very common in some MVC frameworks, such as Rails, Django, and, of course, Laravel. It is through this type of file that we can keep our database consistent with our application, since we cannot versioning the database schemes . Migration files help us to store each change in our database, so that we can version these files and keep the project consistent.
Database seeds serve to populate the tables of a database with an initial batch of records; this is extremely useful when we are developing web applications from the beginning. The data of the initial load can be varied, from tables of users to administration objects such as passwords and tokens, and everything else that we require.
Let's look at how we can create a migration file for the Bands model in Laravel:
- Open your Terminal window and type the following command:
php artisan make:migration create_bands_table
- Open the database/migrations folder, and you will see a file called<timestamp>create_bands_table.php.
- Open this file and paste the following code inside public function up():
Schema::create('bands', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('description');
$table->timestamps();
});
- Paste the following code inside public function down():
Schema::dropIfExists('bands');
- The final result will be the following code:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateBandsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('bands', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('description');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('bands');
}
}
- Inside of the database/factories folder, open the ModalFactory.php file and add the following code, right after the User Factory. Note that we are using a PHP library called faker inside a factory function, in order to generate some data:
$factory->define(App\Band::class, function (Faker\Generator $faker) {
return [
'name' => $faker->word,
'description' => $faker->sentence
];
});
- Go back to your Terminal window and create a database seed. To do this, type the following command:
php artisan make:seeder BandsTableSeeder
- In the database/seeds folder, open the BandsTableSeeder.php file and type the following code, inside public function run():
factory(App\Band::class,5)->create()->each(function ($p) {
$p->save();
});
- Now, in the database/seeds folder, open the DatabaseSeeder.php file and add the following code, inside public function run():
$this->call(BandsTableSeeder::class);
Before we go any further , we need to do a small refactoring on the Band model.
- Inside of the app root, open the Band.php file and add the following code, inside the Band class:
protected $fillable = ['name','description'];
- Go back to your Terminal and type the following command:
php artisan migrate
After the command, you will see the following message in the Terminal window:
Migration table created successfully.
The preceding command was just to populate the database with our seed.
- Go back to your Terminal and type the following command:
php artisan db:seed
We now have five items ready to use in our database.
Let's check whether everything will go smoothly.
- Inside of your Terminal, to exit php-fpm container, type the following command:
exit
- Now, in the application root folder, type the following command in your Terminal:
docker-compose exec mysql mysql -ularavel-angular-book -p123456
The preceding command will give you access to the MySQL console inside mysql Docker container, almost exactly the same as how we gained access to php-fpm container.
- Inside of the Terminal, type the following command to see all of the databases:
show databases;
As you can see, we have two tables: information_schema and laravel-angular-book.
- Let's access the laravel-angular-book table; type the following command:
use laravel-angular-book;
- And now, let's check our tables, as follows:
show tables;
- Now, let's SELECT all records from the bands tables:
SELECT * from bands;
We will see something similar to the following screenshot:
Database bands table
- Now, exit the MySQL console with the following command:
exit