マイグレーション
マイグレーション
Section titled “マイグレーション”マイグレーションは、データベーススキーマのバージョン管理システムです。
マイグレーションの作成
Section titled “マイグレーションの作成”# マイグレーションファイルの作成php artisan make:migration create_users_table
# モデルとマイグレーションを同時に作成php artisan make:model User -mマイグレーションファイル
Section titled “マイグレーションファイル”<?phpuse Illuminate\Database\Migrations\Migration;use Illuminate\Database\Schema\Blueprint;use Illuminate\Support\Facades\Schema;
return new class extends Migration{ public function up() { Schema::create('users', function (Blueprint $table) { $table->id(); $table->string('name'); $table->string('email')->unique(); $table->timestamp('email_verified_at')->nullable(); $table->string('password'); $table->rememberToken(); $table->timestamps(); }); }
public function down() { Schema::dropIfExists('users'); }};マイグレーションの実行
Section titled “マイグレーションの実行”# マイグレーションの実行php artisan migrate
# ロールバックphp artisan migrate:rollback
# すべてのマイグレーションをロールバックphp artisan migrate:reset
# ロールバックして再実行php artisan migrate:refresh
# ロールバックして再実行(データも削除)php artisan migrate:freshカラムの追加
Section titled “カラムの追加”<?php// マイグレーション: カラムの追加public function up(){ Schema::table('users', function (Blueprint $table) { $table->string('phone')->nullable()->after('email'); $table->boolean('is_admin')->default(false); });}