Database Migrations — Laravel

A migration is a PHP file that writes down one change to the database's tables. It holds up() (the change) and down() (its opposite), and the file is made…

Back in plain PHP, you made tables in phpMyAdmin (a web app for managing a MySQL database). A table is where a database stores data, in rows and columns: one row for each book, one column for each kind of data, like the title. The steps in phpMyAdmin were: click, type the column names, save. Then your teammate clones (copies) the project from Git and the first page shows an error right away, because the books table only exists on your laptop. You send a .sql file over WhatsApp, and a week later nobody remembers who already added the isbn column.

Migrations replace those clicks with code. Each migration is a PHP file that writes down one change to the database's structure, for example creating a table, adding a column, or dropping a column. Structure means the shape of its tables, not the data in them. Laravel runs these files in order, then records which files have run. As a result, the database on your laptop, on your teammate's laptop and on the server all have exactly the same tables.

Picture a renovation plan with numbered steps, plus a checklist on the wall. The builder only does the steps that aren't ticked yet, then ticks them. A ticked step is never done again, even if you change what the plan says. In Laravel each step of the plan is one migration file, the builder is Artisan, and the checklist is a table called migrations.

Creating a migration file

Up to the Blade lesson, BookController kept the books in a PHP array, so a new book was gone as soon as the request ended. Now the books move into the database, into a books table. Make the file with Artisan:

php artisan make:migration create_books_table

The terminal answers:

   INFO  Migration [/home/budi/library/database/migrations/2026_10_09_090000_create_books_table.php] created successfully.

Inside the file: up() and down()

This is the file after the book columns are added:

<?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('books', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->string('author');
            $table->integer('stock')->default(0);
            $table->string('isbn')->nullable();
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('books');
    }
};

Laravel turns the Blueprint into SQL (commands for the database) for the database you use. This is exactly what Laravel 13 produces:

Blueprint MySQL SQLite
$table->id() bigint unsigned not null auto_increment primary key integer primary key autoincrement not null
$table->string('title') varchar(255) not null varchar not null
->integer('stock')->default(0) int not null default '0' integer not null default '0'
->string('isbn')->nullable() varchar(255) null varchar
->boolean('borrowable')->default(true) tinyint(1) not null default '1' tinyint(1) not null default '1'
$table->timestamps() created_at and updated_at, timestamp null created_at and updated_at, datetime

Running it: migrate, status, rollback

These are three commands to run, check and undo migrations, with the terminal's answers:

$ php artisan migrate

   INFO  Preparing database.

  Creating migration table ....................................... 3.51ms DONE

   INFO  Running migrations.

  2026_10_09_090000_create_books_table ........................... 1.99ms DONE

$ php artisan migrate:status

  Migration name .............................................. Batch / Status
  2026_10_09_090000_create_books_table ............................... [1] Ran

$ php artisan migrate:rollback

   INFO  Rolling back migrations.

  2026_10_09_090000_create_books_table ........................... 0.85ms DONE

Laravel only matches file names

Say the books table is already migrated. Then you remove ->nullable() from the isbn column in that same file and run migrate again. Many people expect isbn to be required now.

Before running anything, migrate reads the names in the migrations table. A file whose name is already there is skipped, whatever it says now. So removing ->nullable() and running migrate again only answers:

   INFO  Nothing to migrate.

The isbn column in the database can still be empty. There are two right ways to change the table:

// up()
Schema::table('books', function (Blueprint $table) {
    $table->integer('year')->nullable();
});

// down()
Schema::table('books', function (Blueprint $table) {
    $table->dropColumn('year');
});

Schema::table changes an existing table instead of creating a new one. The new column is nullable(), because the books already saved have no publication year yet. On SQLite, this change becomes alter table "books" add column "year" integer. How to change columns in production (the server that real users use) without breaking anything is covered in the Schema Migration Patterns lesson.

nullable() and default(): required or optional

Try saving a book with only a title and an author (this Eloquent code is covered in the next lesson):

Book::create(['title' => 'The Rainbow Troops', 'author' => 'Andrea Hirata']);

The result depends on the isbn column in the migration:

isbn in the migration What happens
->nullable() saved: stock is 0 from default(0), isbn is NULL
no ->nullable() refused: SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: books.isbn

The database refuses it, not Laravel. Every column that may be left out needs ->nullable() or ->default(...).

⚠️ Migrations that do nothing, rollbacks that fail

TL;DR: A migration is a PHP file that writes down one change to the database's tables. It holds up() (the change) and down() (its opposite), and the file is made with php artisan make:migration create_books_table. Inside Schema::create (which creates a table), you add columns with $table->id(), string() (text up to 255 on MySQL and PostgreSQL, no limit on SQLite), integer(), boolean() and timestamps() (two columns: created_at and updated_at). Every column is required unless it gets ->nullable() or ->default(...). php artisan migrate runs the files not yet recorded in the migrations table and gives them a batch number (a group number), migrate:status shows Ran (already run) or Pending (not yet), and migrate:rollback undoes the last batch. A recorded file never runs again, so the next change goes in a new migration (Schema::table).

⚡ A table written as code

Write:

Schema::create('books', function (Blueprint $table) {
    $table->id();
    $table->string('title');
    $table->integer('stock')->default(0);
    $table->string('isbn')->nullable();
    $table->timestamps();
});

Meaning: A migration is a PHP file that creates or changes a database table with code. Schema::create('books', ...) creates the books table, and each $table->... line adds one column. Every column is required unless it gets ->nullable() or ->default(...). timestamps() (with an s) makes two columns at once: created_at and updated_at.

In the exercise, the checker looks for $table->timestamps() in your code.

⚡ Run it with Artisan

Write:

php artisan make:migration create_books_table
php artisan migrate

Meaning: make:migration creates a new migration file. migrate runs up() (the part that holds the change) for every file not yet recorded in the migrations table, then records it with a batch number (a group number). migrate:rollback runs down() (the opposite of up()) for the last batch.

⚡ A file that ran never runs again

Guess first: you remove ->nullable() from the isbn column in a migration that already ran, then run migrate again. What happens?

Nothing to migrate. Laravel only matches file names, and that name is already recorded in the migrations table. The isbn column in the database can still be empty.

Meaning: The next change goes in a new migration with Schema::table, which changes an existing table. If the file only exists on your laptop, you may migrate:rollback first, edit it, then migrate again.

What you will learn