Skip to content

マイグレーション

ポケモンで例えると: Laravelを理解することは、新しい「わざ」を覚えるようなもの——使いこなせば、より強力な開発者になれます。

マイグレーションは、データベーススキーマのバージョン管理システムです。

Terminal window
# マイグレーションファイルの作成
php artisan make:migration create_users_table
# モデルとマイグレーションを同時に作成
php artisan make:model User -m
database/migrations/2024_01_01_000000_create_users_table.php
<?php
use 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');
}
};
Terminal window
# マイグレーションの実行
php artisan migrate
# ロールバック
php artisan migrate:rollback
# すべてのマイグレーションをロールバック
php artisan migrate:reset
# ロールバックして再実行
php artisan migrate:refresh
# ロールバックして再実行(データも削除)
php artisan migrate:fresh
<?php
// マイグレーション: カラムの追加
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('phone')->nullable()->after('email');
$table->boolean('is_admin')->default(false);
});
}

「Laravelは、開発者としての成長に欠かせない『わざ』である——継続的に学び、実践することで真の力となる。」

技術の習得は、ポケモンのレベルアップと同じ——一歩ずつ、着実に成長していくことが重要です。Laravelをマスターし、より強力な開発者を目指しましょう。