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.
- The file name starts with a date and time (
2026_10_09_090000). Laravel runs migrations in order, starting with the oldest date. - From the name
create_books_table, Laravel guesses that you want to create abookstable. So the file already holdsSchema::create('books', ...)with$table->id()and$table->timestamps(). You add the other columns yourself.
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');
}
};
return new class extends Migrationmakes a class with no name. Laravel uses the object that this filereturns, so the class needs no name.up()holds the change.down()holds its opposite, used when the change is undone (a rollback).Schema::create('books', ...)creates thebookstable.$tableis a Blueprint, a kind of floor plan of the table: each$table->...line adds one column.$table->id()makes theidcolumn, a number that counts up by itself for each new row and is the primary key (the number that identifies each row, covered in the Primary Key & Foreign Key lesson).$table->string('title')makes a short text column, at most 255 characters on MySQL and PostgreSQL. SQLite, the default database of a new Laravel project, doesn't limit the length. So limit it in validation (checking the input) too, withmax:255. For another length, writestring('title', 100).$table->integer('stock')->default(0)makes a whole-number column for the number of copies.->default(0)fills it with 0 when no value is given.$table->string('isbn')->nullable()makes a column that may be empty (NULL, meaning no value), since old books often have no ISBN. Without->nullable()or->default(...), every column is required.- For true or false there's
$table->boolean(...), for example$table->boolean('borrowable')->default(true): aborrowable(may be borrowed) column that is true by default. $table->timestamps()(with an s) makes two columns at once,created_atandupdated_at. Later, Eloquent (Laravel's way to read and save data, in the next lesson) fills both in by itself.Schema::dropIfExists('books')drops the table, if it exists.
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
php artisan migraterunsup()for every file that isn't recorded yet. The very first time it runs, this command creates themigrationstable first (the line "Creating migration table").- Once
up()succeeds, the file's name is recorded in themigrationstable with a batch number (a group number). Onemigraterun counts as one batch, however many files run in it. php artisan migrate:statuscompares thedatabase/migrationsfolder with that table:[1] Ranmeans it ran in batch 1,Pendingmeans it hasn't run.php artisan migrate:rollbackrunsdown()for the files in the last batch, then deletes their records. Their status goes back toPending.php artisan migrate:freshdrops every table, then runs every migration from the start. Use it only for test data on your laptop.
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:
- Nobody else has the file yet (only on your laptop, not pushed, meaning not sent to the shared Git copy yet):
migrate:rollback, edit the file, thenmigrateagain. - A teammate or the server already ran it: leave the old file alone. Make a new migration. The example below is for another change, adding a publication year column. Its file is made with
php artisan make:migration add_year_to_books_table, and it holds:
// 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
- Editing a migration that already ran. You get
Nothing to migrate.and your database doesn't change. Roll back first (if the file is only on your laptop), or make a new migration. - A
down()that doesn't undoup(). Ifup()creates a table,down()must drop the same table withSchema::dropIfExists(...). Otherwise the rollback fails or leaves part of the change in the database. migrate:freshon a server. Every table is dropped, and all the data is gone.timestampvstimestamps.$table->timestamp('created_at')makes only one column.$table->timestamps()makes both. In the exercise, the checker looks for$table->timestamps()in your code.
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.