
もくじ
テーブルの新規作成
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateSampleAdminUserTables extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
const TABLE_NAME = "sample_admin_users";
public function up()
{
if (Schema::hasTable(self::TABLE_NAME)) {
return;
}
Schema::create(self::TABLE_NAME, function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('user_id')->unsigned();
$table->timestamps();
$table->foreign('user_id', 'sau_idfk_users')
->references('id')
->on('users')
->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table(self::TABLE_NAME, function (Blueprint $table) {
$table->dropForeign('sau_idfk_users');
});
Schema::drop(self::TABLE_NAME);
}
}
既存テーブルへのカラム追加
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class ModifySampleJobs extends Migration
{
const TABLE_NAME = 'sample_jobs';
const COLUMN_NAME_SKILL_ID_ID = 'skill_id';
const COLUMN_NAME_STATUS = 'status';
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
if (!Schema::hasTable(self::TABLE_NAME) ||
!Schema::hasColumn(self::TABLE_NAME, self::COLUMN_NAME_SKILL_ID) ||
!Schema::hasColumn(self::TABLE_NAME, self::COLUMN_NAME_STATUS)
) {
return;
}
Schema::table(self::TABLE_NAME, function (Blueprint $table) {
$table->bigInteger(self::COLUMN_NAME_SKILL_ID)->unsigned();
$table->enum(self::COLUMN_NAME_STATUS, ['ok', 'soso', 'bad'])->default('ok')->comment('ステータス');
$table->foreign(self::COLUMN_NAME_SKILL_ID, 'sj_idfk_skills')
->references('id')
->on('skills')
->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
if (!Schema::hasTable(self::TABLE_NAME) ||
!Schema::hasColumn(self::TABLE_NAME, self::COLUMN_NAME_SKILL_ID) ||
!Schema::hasColumn(self::TABLE_NAME, self::COLUMN_NAME_STATUS)
) {
return;
}
Schema::table(self::TABLE_NAME, function (Blueprint $table) {
$table->dropColumn(self::COLUMN_NAME_SKILL_ID);
$table->dropColumn(self::COLUMN_NAME_STATUS);
});
}
}
Enum型が含まれるテーブルのカラムの変更
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class ModifyUserSampleTables extends Migration
{
const TABLE_NAME = 'user_samples';
const COLUMN_NAME_END_DATE = 'end_date';
public function __construct()
{
\DB::getDoctrineSchemaManager()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');
}
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
if (!Schema::hasTable(self::TABLE_NAME) || !Schema::hasColumn(self::TABLE_NAME, self::COLUMN_NAME_END_DATE)) {
return;
}
Schema::table(self::TABLE_NAME, function (Blueprint $table) {
$table->datetime(self::COLUMN_NAME_END_DATE)->change();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
if (!Schema::hasTable(self::TABLE_NAME) || !Schema::hasColumn(self::TABLE_NAME, self::COLUMN_NAME_END_DATE)) {
return;
}
Schema::table(self::TABLE_NAME, function (Blueprint $table) {
$table->date(self::COLUMN_NAME_END_DATE)->change();
});
}
}
外部キー制約について
削除時に子テーブルを削除するのか、NULLにするか設定できます。
削除
Schema::table(self::TABLE_NAME, function (Blueprint $table) {
$table->bigInteger(self::COLUMN_NAME_SKILL_ID)->unsigned();
$table->enum(self::COLUMN_NAME_STATUS, ['ok', 'soso', 'bad'])->default('ok')->comment('ステータス');
$table->foreign(self::COLUMN_NAME_SKILL_ID, 'sj_idfk_skills')
->references('id')
->on('skills')
->onDelete('cascade');
});
NULL
Schema::table(self::TABLE_NAME, function (Blueprint $table) {
$table->bigInteger(self::COLUMN_NAME_SKILL_ID)->unsigned();
$table->enum(self::COLUMN_NAME_STATUS, ['ok', 'soso', 'bad'])->default('ok')->comment('ステータス');
$table->foreign(self::COLUMN_NAME_SKILL_ID, 'sj_idfk_skills')
->references('id')
->on('skills')
->onDelete('set null');
});
論理削除
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUserRecieveEmailAddresses extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
const TABLE_NAME = "user_recieve_email_addresses";
public function up()
{
if (Schema::hasTable(self::TABLE_NAME)) {
return;
}
Schema::create(self::TABLE_NAME, function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('user_id')->unsigned();
$table->string('email_address')->comment('メールアドレス');
$table->enum('status', ['NotStarted', 'Pending', 'Processing', 'Success', 'Failed', 'TemporaryFailure', 'Unknown'])->default('NotStarted')->comment('認証ステータス');
$table->timestamps();
$table->softDeletes();
$table->foreign('user_id', 'urea_idfk_users')
->references('id')
->on('users')
->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop(self::TABLE_NAME);
}
}
Model(Entity)で、SoftDeletesをuseする必要がある
use SoftDeletes;
app\Http\Entities\UserRecieveEmailAddress.php
namespace App\Entities;
use Illuminate\Database\Eloquent\Model;
use Prettus\Repository\Contracts\Transformable;
use Prettus\Repository\Traits\TransformableTrait;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* Class Digest.
*
* @package namespace App\Entities;
*/
class UserRecieveEmailAddress extends Model implements Transformable
{
use TransformableTrait;
use SoftDeletes;
const STATUSES = [
'pending' => "Pending",
'processing' => "Processing",
'success' => "Success",
'failed' => "Failed",
'temporary_failure' => "TemporaryFailure",
'unknown' => "Unknown",
];
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $guarded = ['id'];
}

