Migrations

Contents
Make a migration
Schema builder
Run it
Roll back

Make a migration

Migrations are versioned instructions for building tables. Each migration file has a timestamped name, so the team applies them in the same order.

Generate a posts table migration

php artisan make:migration create_posts_table

INFO Migration file created in database/migrations.

Open the new file in database/migrations. It contains empty up and down methods waiting for your schema.

Schema builder

The up method describes the table to create, and the down method describes how to remove it. The Schema builder turns that description into SQL for any supported database.

The posts table below gets an auto-incrementing id, a title column, and timestamp columns for creation and update times.

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->increments("id"); $table->string("title"); $table->timestamps(); }); } public function down(): void { Schema::dropIfExists("posts"); } };

Run it

The migrate command applies every migration that has not run yet. Laravel records the batch in a migrations table, so re-running the command is safe.

Apply pending migrations

php artisan migrate

INFO Running migrations. create_posts_table done.

Roll back

The rollback command undoes the last batch by calling each down method. Use it right after a bad migration, while the mistake is still fresh and local.

Fix the migration file after rolling back, then migrate again. Never edit migrations that already ran on a shared server.

Undo the last batch

php artisan migrate:rollback

INFO Rolling back migrations. create_posts_table rolled back.

Next: Eloquent models

Article author: Arthur Isaev

Related articles
Development with PHP
What is Laravel and where it runs
Install Laravel
Your first route
Controllers
Blade basics
Blade advanced
Migrations
Eloquent models
Relationships
Query builder
Seeders and factories
Validation
Auth intro
Hand-rolled login
Middleware
Service container
Config and env
Artisan commands
Events and listeners
Queues
Mail
Cache
Sessions in Laravel
CSRF
Hashing
Sanctum tokens
API resources
Gates and policies
Notifications
Scheduler
Filesystem
Localization
Testing
HTTP tests
Collections
Carbon dates
Pagination
File uploads
Eloquent advanced
Transactions and locks
Deployment
Debugbar and dd
Broadcasting
Full-text search
Capstone: blog
Capstone: JSON API
Capstone: shop cart
Performance: kill N+1
Modern Laravel tour
Hero roadmap: the whole course on one page
Banner image

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: