Skip to content

Instantly share code, notes, and snippets.

@tournasdim
Created August 26, 2013 10:59
Show Gist options
  • Save tournasdim/6340314 to your computer and use it in GitHub Desktop.
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)
<?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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment