Launch offer
Business website $150 USD Custom plugin $200 USD Ready in 5 days
Get a quote
Laravel

Database Configuration and Migrations in Laravel

Configure .env database settings and understand how Laravel migrations version your schema.

Database Configuration and Migrations in Laravel — a practical guide to Laravel database migrations with clear examples you can reuse in real projects.

Laravel Tutorial Series (7/27). Prefer one page? Read the complete Laravel tutorial (all 27 topics).

Short description

Migrations are version-controlled PHP classes that create and change database tables safely across environments.

Configure database in `.env`

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=blog_app
DB_USERNAME=root
DB_PASSWORD=

Create a migration

php artisan make:migration create_posts_table

Migration anatomy

public function up(): void
{
    Schema::create('posts', function (Blueprint $table) {
        $table->id();
        $table->string('title');
        $table->text('body');
        $table->timestamps();
    });
}

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

Run migrations

php artisan migrate
php artisan migrate:status
php artisan migrate:rollback

Tip

Never edit production data by hand when a migration can express the change.

Leave a reply

Your email address will not be published. Required fields are marked *