Friday, July 15, 2016

Laravel Migration and Seeding

Migration

Migrations are like version control of the database. Migrations are a series of time stamped instructions for making changes to your database. A migration file contains a class which have a series of SQL methods for adding tables, updating tables or dropping tables.
Migration file will have two methods
up method - Make changes to the database
down method - Roll back the changes
When a migration run, it is recorded in a 'migrations' table in the database.Laravel knows which migrations have already run and which all are pending.
First we need to create the migrations table in the database, run the following command from the terminal.

php artisan migrate:install

This will create a migrations table in your database.
Creating Migrations

php artisan make:migration create_accounts_table --create=accounts

create_accounts_table - migration name
Migration file will be placed in your database/migrations directory. Each migration file name contains a timestamp which allows laravel to determine the order of the migrations. (Eg: 2016_01_28_103708_create_accounts_table.php)
migration file will have the following
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateAccountsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('accounts', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('password', 60);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
        Schema::drop('accounts');
}
}
When the migration is run,the instructions in the up method are run. When the migration is rolled back, the instructions in the down methods are run

Run the migrate command from the terminal
php artisan migrate
Laravel will create a account table in the database

Seeding

Applications need some data loaded inorder to run successfully in development and test. Laravel introduced a simple method of seeding the database with test data using seed classes. Seed classes are stored in database/seeds directory. By default, a DatabaseSeeder class is defined for you.
Run the follwing command from your terminal to generate the seeder
php artisan make:seeder AccountsTableSeeder
It will create the AccountsTableSeeder.php file in the database/seeds directory
File will have the following
<?php
use Illuminate\Database\Seeder;
class AccountsTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
DB::table('accounts')->insert([
'name' => str_random(10),
'password' => bcrypt('secret'),
]);
}
}
Issue the following command on your terminal to run the seeding
php artisan db:seed --class=AccountsTableSeeder

Using modal factories

Instead of manually specifying the attributes for each model, you can use modal factories to generate large amount of records in the database.
  • Create the model class. Run the following command from your terminal
php artisan make:model account
   It will create the model class in the app directory
  • Define your model factory in the ModelFactory (database/factories/ModelFactories.php)

$factory->define(App\account::class, function (Faker\Generator $faker) {
return [
'name' => $faker->name,
'password' => bcrypt(str_random(10)),
];
});
  • Make the necessary changes in the AccountTableSeeder file
<?php
use Illuminate\Database\Seeder;
class AccountsTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
factory(App\account::class, 50)->create()->each(function($u) {
$u->posts()->save(factory(App\Post::class)->make());
});
}
}
  • Run the following command
php artisan make:seeder AccountsTableSeeder
(Will create 50 records in the database)

No comments:

Post a Comment