Created
August 26, 2013 10:59
-
-
Save tournasdim/6340314 to your computer and use it in GitHub Desktop.
L4 Migration and seed example (a full example , from generating migration / seed files upto seeding to db with CLI)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' => '[email protected]', | |
'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 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment