php artisan make:migration create_posts_table
php artisan make:migration create_categories_table
php artisan make:migration create_comments_table
database/migrationsに新たに3つファイルが作成された、それぞれ以下のように編集
2017_05_23_143039_create_posts_table
public function up()
{
Schema::create('posts', function($table){
$table->increments('id');
$table->string('title');
$table->string('cat_id'); // ポストテーブルとカテゴリーテーブルの紐付けに利用します
$table->text('content');
$table->unsignedInteger('comment_count'); // 投稿に何件のコメントがついたのかをカウントします
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('posts');
}
2017_05_23_143100_create_categories_table
public function up()
{
Schema::create('categories', function($table){
$table->increments('id');
$table->string('name');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('categories');
}
2017_05_23_143113_create_comments_tablepublic function up()
{
Schema::create('comments', function($table){
$table->increments('id');
$table->unsignedInteger('post_id'); // ポストテーブルとコメントテーブルの紐付けに利用します
$table->string('commenter');
$table->text('comment');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('comments');
}
php artisan migrate でtableが作成される下記エラーになる場合、対応が必要。(mysql5.7.7以降のバージョンなら対応不要)
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes
app/Providers/AppServiceProvider.php に以下のように追加
use Illuminate\Support\Facades\Schema;
public function boot()
{
Schema::defaultStringLength(191);
}
参考:https://manablog.org/laravel_bulletin_board/
https://laravel-news.com/laravel-5-4-key-too-long-error
0 件のコメント:
コメントを投稿