Created
December 13, 2017 06:35
-
-
Save RyosukeKamei/98122251cfe23cd0e439ff43b151998d to your computer and use it in GitHub Desktop.
開発における悩ましいデータベース共有問題を解決するLaravelのマイグレーションとシーダー解説編~アジャイルでDevOpsなシステム構築実践~ ref: https://qiita.com/RyosukeKamei/items/832467f44b95799c8bd9
This file contains 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 | |
use Illuminate\Support\Facades\Schema; | |
use Illuminate\Database\Schema\Blueprint; | |
use Illuminate\Database\Migrations\Migration; | |
class CreateRoundsTable extends Migration | |
{ | |
/** | |
* マイグレーション実行 | |
* | |
* @return void | |
*/ | |
public function up() | |
{ | |
Schema::create('rounds', function (Blueprint $table) { | |
$table | |
->increments('id') | |
->comment('ID'); | |
$table | |
->string('name') | |
->comment('試験実施回名'); | |
$table | |
->timestamp('created_at') | |
->default(DB::raw('CURRENT_TIMESTAMP')) | |
->comment('登録日'); | |
$table | |
->timestamp('updated_at') | |
->default(DB::raw('CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP')) | |
->comment('更新日'); | |
}); | |
} | |
/** | |
* ロールバック時に実行 | |
* | |
* @return void | |
*/ | |
public function down() | |
{ | |
Schema::dropIfExists('rounds'); | |
} | |
} |
This file contains 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 | |
use Illuminate\Support\Facades\Schema; | |
use Illuminate\Database\Schema\Blueprint; | |
use Illuminate\Database\Migrations\Migration; | |
class CreateQuestionsTable extends Migration | |
{ | |
/** | |
* マイグレーション実行 | |
* | |
* @return void | |
*/ | |
public function up() | |
{ | |
Schema::create('questions', function (Blueprint $table) { | |
$table | |
->increments('id') | |
->comment('ID'); | |
$table | |
->integer('number') | |
->unsigned() | |
->comment('問題番号'); | |
$table | |
->text('body') | |
->comment('問題文'); | |
$table | |
->text('commentary') | |
->comment('解説'); | |
$table | |
->integer('firstcategory_id') | |
->unsigned() | |
->comment('小項目ID 外部参照 firstcategories.id'); | |
$table | |
->integer('divition_id') | |
->unsigned() | |
->comment('問題種別ID 外部参照 divitions.id'); | |
$table | |
->integer('round_id') | |
->unsigned() | |
->comment('試験実施回ID 外部参照 rounds.id'); | |
$table | |
->integer('examination_id') | |
->unsigned() | |
->comment('試験区分ID 外部参照 examinations.id'); | |
$table | |
->timestamp('created_at') | |
->default(DB::raw('CURRENT_TIMESTAMP')) | |
->comment('登録日'); | |
$table | |
->timestamp('updated_at') | |
->default(DB::raw('CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP')) | |
->comment('更新日'); | |
/* | |
* 外部参照制約 小項目 | |
*/ | |
$table | |
->foreign('firstcategory_id') | |
->references('id') | |
->on('firstcategories') | |
->onDelete('RESTRICT') | |
->onUpdate('RESTRICT'); | |
/* | |
* 外部参照制約 問題種別 | |
*/ | |
$table | |
->foreign('divition_id') | |
->references('id') | |
->on('divitions') | |
->onDelete('RESTRICT') | |
->onUpdate('RESTRICT'); | |
/* | |
* 外部参照制約 試験実施回 | |
*/ | |
$table | |
->foreign('round_id') | |
->references('id') | |
->on('rounds') | |
->onDelete('RESTRICT') | |
->onUpdate('RESTRICT'); | |
/* | |
* 外部参照制約 試験区分 | |
*/ | |
$table | |
->foreign('examination_id') | |
->references('id') | |
->on('examinations') | |
->onDelete('RESTRICT') | |
->onUpdate('RESTRICT'); | |
}); | |
} | |
/** | |
* ロールバック時に実行 | |
* | |
* @return void | |
*/ | |
public function down() | |
{ | |
Schema::dropIfExists('questions'); | |
} | |
} | |
This file contains 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 | |
namespace App; | |
use Illuminate\Database\Eloquent\Model; | |
class Question extends Model | |
{ | |
/* | |
* belongsTo設定 | |
* 小項目を参照 | |
*/ | |
public function firstcategory() | |
{ | |
return $this->belongsTo('App\Firstcategory'); | |
} | |
/* | |
* belongsTo設定 | |
* 問題種別を参照 | |
*/ | |
public function divition() | |
{ | |
return $this->belongsTo('App\Divition'); | |
} | |
/* | |
* belongsTo設定 | |
* 試験実施回を参照 | |
*/ | |
public function round() | |
{ | |
return $this->belongsTo('App\Round'); | |
} | |
/* | |
* belongsTo設定 | |
* 試験区分を参照 | |
*/ | |
public function examination() | |
{ | |
return $this->belongsTo('App\Examination'); | |
} | |
} |
This file contains 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 | |
namespace App; | |
use Illuminate\Database\Eloquent\Model; | |
class Round extends Model | |
{ | |
// | |
} | |
This file contains 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 | |
use Illuminate\Database\Seeder; | |
/* | |
* Eloquentを利用するのでRoundモデルを使う | |
*/ | |
use App\Round; | |
class RoundsTableSeeder extends Seeder | |
{ | |
/** | |
* シーダーを実行 | |
* | |
* @return void | |
*/ | |
public function run() | |
{ | |
/* | |
* 必要な情報を配列化 | |
*/ | |
$round_names[] = '平成21年度春'; | |
$round_names[] = '平成21年度秋'; | |
$round_names[] = '平成22年度春'; | |
$round_names[] = '平成22年度秋'; | |
$round_names[] = '平成23年度特別'; | |
$round_names[] = '平成23年度秋'; | |
$round_names[] = '平成24年度春'; | |
$round_names[] = '平成24年度秋'; | |
$round_names[] = '平成25年度春'; | |
$round_names[] = '平成25年度秋'; | |
$round_names[] = '平成26年度春'; | |
$round_names[] = '平成26年度秋'; | |
$round_names[] = '平成27年度春'; | |
$round_names[] = '平成27年度秋'; | |
$round_names[] = '平成28年度春'; | |
$round_names[] = '平成28年度秋'; | |
$round_names[] = '平成29年度春'; | |
$round_names[] = '平成29年度秋'; | |
/* | |
* ループしてデータを作成 | |
*/ | |
foreach($round_names as $round_name) | |
{ | |
Round::create([ | |
'name' => $round_name, | |
]); | |
} | |
} | |
} |
This file contains 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 artisan make:seeder RoundsTableSeeder | |
Seeder created successfully. |
This file contains 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 artisan db:seed --class=ExaminationsTableSeeder | |
(特に成功メッセージなし) |
This file contains 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 artisan migrate | |
Migrating: 2017_12_12_224229_create_rounds_table | |
Migrated: 2017_12_12_224229_create_rounds_table |
This file contains 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 artisan make:model {モデル名} -m |
This file contains 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 artisan make:model round -m | |
Model created successfully. | |
Created Migration: 2017_12_12_231913_create_rounds_table |
This file contains 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
$table | |
->foreign('firstcategory_id') | |
->references('id') | |
->on('firstcategories') | |
->onDelete('RESTRICT') | |
->onUpdate('RESTRICT'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment