use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration;
class CreateAuthorsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('authors', function($table) { $table->increments('id'); // id INT AUTO_INCREMENT PRIMARY KEY $table->string('title', 100); // title VARCHAR(100) $table->text('body'); // body TEXT $table->timestamps(); // created_at TIMESTAMP, updated_at TIMESTAMP }); }
/** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('authors'); } }
이렇게 코드를 작성하고 php artisan migrate 하면 정상적으로 데이터 베이스에 테이블들이 추가가됩니다
컬럼을 추가 하고싶어서
$ php artisan make:migration add_name_to_authors_table 이렇게 마이그레이션을 만든다음에
2017_09_28_214456_create_authors_table.php
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateAuthorsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('authors', function($table) {
$table->increments('id'); // id INT AUTO_INCREMENT PRIMARY KEY
$table->string('title', 100); // title VARCHAR(100)
$table->text('body'); // body TEXT
$table->timestamps(); // created_at TIMESTAMP, updated_at TIMESTAMP
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('authors');
}
}
이렇게 코드를 작성하고 php artisan migrate 하면 정상적으로 데이터 베이스에 테이블들이 추가가됩니다
컬럼을 추가 하고싶어서
$ php artisan make:migration add_name_to_authors_table 이렇게 마이그레이션을 만든다음에
2017_09_28_223618_add_name_to_authors_table.php 파일에
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddNameToAuthorsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('authors', function(Blueprint $table) {
$table->string('name')->after('email')->nullable(); // nullable()은 NULL 을 허용한다는 얘기
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('authors', function(Blueprint $table) {
$table->dropColumn('name');
});
}
}
이렇게 코드를 작성하고 php artisan migrate 했는데
[Illuminate\Database\QueryException]
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'email' in 'authors' (SQL: alter table `authors` add `name`
varchar(255) null after `email`)
[PDOException]
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'email' in 'authors'
이런오류가 나오는데 어쩌죠 ㅠㅠ 글이 긴점 죄송합니다만 너무 답답해서 자세히 올리려 글이 길어졌어요 ㅠㅠ
서버 설정이 잘못해줬을까요 ?? 아무리 확인해도 코드는 틀린점이없는것같은데 이상합니다 ㅠㅠ
도와주세요 ㅠㅠ