Migrations
Migrations version database schema changes.
What it is
Migrations version database schema changes.
When to use it
Define constraints alongside application validation.
Syntax
Schema::create("orders", function (Blueprint $table) {
$table->id();
$table->foreignId("user_id")->constrained()->restrictOnDelete();
$table->string("reference")->unique();
$table->bigInteger("total_cents");
$table->timestamps();
});Example
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration {
public function up(): void {
Schema::create("orders", function (Blueprint $table) {
$table->id();
$table->foreignId("user_id")->constrained()->restrictOnDelete();
$table->string("reference")->unique();
$table->bigInteger("total_cents");
$table->string("status")->default("pending");
$table->timestamps();
$table->index(["user_id", "status"]);
});
}
public function down(): void { Schema::dropIfExists("orders"); }
};Common mistakes
Check engine-specific CHECK and ALTER support; financial records rarely warrant cascading deletion.