tournasdim
8/26/2013 - 10:59 AM

L4 Migration and seed example (a full example , from generating migration / seed files upto seeding to db with CLI)

L4 Migration and seed example (a full example , from generating migration / seed files upto seeding to db with CLI)

<?php

php artisan migrate:make create_user_table

//  app/database/migrations folder 
<?php
use Illuminate\Database\Migrations\Migration;
class CreateUserTable extends Migration {
	public function up()
	{
		Schema::create('users', function($table)
		{
			$table->increments('id');
			$table->string('email')->unique();
			$table->string('username');
			$table->string('name');
			$table->string('password');
			$table->timestamps();
		});
	}

	public function down()
	{
		Schema::drop('users');
	}

}



##########################  SEEDING #########################
#  app / database / seeds / DatabaseSeeder.php . This file is by default present
#  Just add which tables will be seeded  
<?php
class DatabaseSeeder extends Seeder {

	public function run()
	{
		$this->call('UserTableSeeder');
		$this->command->info('User table seeded!');
		$this->call('LinkTableSeeder');
		$this->command->info('Link table seeded!');
		$this->call('PostTableSeeder');
		$this->command->info('Post table seeded!');
	}

}


// manually create the seeds file 
<?php
class UserTableSeeder extends Seeder {

    public function run()
    {
        DB::table('users')->delete();
        $user = new User;
        $user->fill(array(
        	'email'	   => 'johndoe@johndoe.com',
        	'username' => 'John',
        	'name'	   => 'John'
        	));

       	$user->password = Hash::make('johndoexyz');

       	$user->save();
    }

}


####  Now migrate the tables and then seed
php artisan migrate
php artisan db:seed 
// Re-migrate the tables and seed in one command
php artisan migrate:refresh --seed