Documentation Index
Fetch the complete documentation index at: https://kawax.biz/llms.txt
Use this file to discover all available pages before exploring further.
What are migrations?
Migrations are like version control for your database. They let your team define and share the application’s database schema definition. If you have ever had to tell a teammate to manually add a column to their local database after pulling your changes, migrations solve exactly that problem. Migration files are stored in thedatabase/migrations directory. Each filename contains a timestamp that Laravel uses to determine the order in which to run the migrations.
Creating a migration
Use themake:migration Artisan command to generate a new migration file:
create_posts_table produces a stub that creates the posts table.
Migration structure
A migration class contains two methods:up and down.
up— adds tables, columns, or indexes to the database.down— reverses the operations performed byup. Called when rolling back.
Available column types
TheBlueprint class provides many column type methods:
| Method | Description |
|---|---|
$table->id() | Auto-incrementing primary key (alias for bigIncrements('id')) |
$table->string('name') | VARCHAR column (255 characters by default) |
$table->text('body') | TEXT column |
$table->integer('count') | INTEGER column |
$table->boolean('active') | TINYINT used to store a boolean value |
$table->timestamp('published_at') | TIMESTAMP column |
$table->timestamps() | Adds created_at and updated_at columns |
$table->softDeletes() | Adds a deleted_at column for soft deletes |
$table->foreignId('user_id') | BIGINT UNSIGNED column for a foreign key |
Column modifiers
Chain modifier methods to customize column definitions:Running migrations
Run all pending migrations with themigrate command:
Rolling back migrations
Roll back the most recent batch of migrations withmigrate:rollback:
--step option:
migrate:refresh drops and recreates all tables, so any existing data is lost. It is useful for resetting your database during development.Practical example: creating a posts table
Here is the complete workflow for creating aposts table for a blog application.
1. Generate the migration file
2. Edit the migration
Opendatabase/migrations/xxxx_xx_xx_xxxxxx_create_posts_table.php and define the schema:
3. Run the migration
posts table is created in your database.
4. Adding a column later
When you need to add a column after a table has already been created, create a new migration instead of modifying the existing one:Next steps
Database seeding
Learn how to populate the tables you created with sample data using seeders.
Eloquent ORM
Learn how to interact with the tables you created using Eloquent.