laravel - เริ่มต้นกับ migration ด้วย 6 ขั้นตอน

Migrations
คือ การเก็บประวัติสร้างจุดเซฟของฐานข้อมูล  โดยสามารถย้อนกลับได้ (rollback)


  • 1.Create migration file in /app/database/migrations
sudo php artisan migrate:make create_users_table --create=users


  • 2-Edit in 
/app/database/migrations/2015_03_05_111744_create_users_table.php


public function up()
   {
     Schema::create('users', function(Blueprint $table) 
   {
     $table->engine = 'InnoDB'; $table->increments('u_id'); $table->string('u_name', 10); $table->string('u_username', 20); $table->string('u_password', 20); $table->string('u_email', 40); $table->string('u_address', 255); $table->timestamps();
   });
}



  • 3-Create Table in Database

php artisan migrate
  • 4-Create new file in  app/database/seeds
File - [UserTableSeeder.php]

<?php

class UserTableSeeder extends Seeder
{

public function run()
{
    DB::table('users')->delete();
    User::create(array(
        'u_name'     => 'ak1',
        'u_username' => 'akone1',
        'u_password'     => Hash::make('awesome'),
        'u_email' => 'akone1@gmail.com',
        'u_address' => 'basic-hack',
    ));
}

}



  • 5.Add New line in app/database/seeds/DatabaseSeeder.php, 

$this->call('UserTableSeeder');




  • 6.insert new user in users table

php artisan db:seed





Testing Rollback or reset
php artisan migrate:rollback
php artisan migrate:reset.





ปล.สำหรับท่านที่เจอ

" Fatal error: Class 'CreateBlogsTable' not found in ......bla bla bla..... "

วิธีแรก
php composer.phar dump-autoload


วิธีที่ 2 changing my "minimum-stability" from "dev" to "stable" in composer.json. Then running composer update.