A migration is a PHP file that describes a database table. It works like version control for your database: every developer and server runs the same migrations and gets the same tables.
Set up the database
In .env set DB_CONNECTION, DB_DATABASE, DB_USERNAME and DB_PASSWORD. Create the empty database in MySQL first.
Create a migration
php artisan make:migration create_posts_table creates a file in database/migrations with two methods:
up()— create or change the tabledown()— undo the change
Common column types
$table->id()— auto-increment primary key$table->string('title')— VARCHAR$table->text('body')— long text$table->boolean('is_published')->default(false)$table->foreignId('user_id')->constrained()— foreign key tousers$table->timestamps()—created_atandupdated_at
Commands
php artisan migrate— run new migrationsphp artisan migrate:rollback— undo the last batchphp artisan migrate:fresh— drop all tables and run again (deletes all data, development only)
Example
Example — Posts table migration
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(): void
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
$table->string('title');
$table->text('body');
$table->boolean('is_published')->default(false);
$table->timestamps();
});
}
public function down(): void
{
Schema::dropIfExists('posts');
}
};
Run php artisan migrate and a posts table appears with id, user_id, title, body, is_published, created_at and updated_at columns.