Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Can Blueprint generate migrations for existing tables
    Laravel Shift Blueprint generate migrations existing database tables Laravel migrations YAML schema Artisan command migration file Laravel application


Can Blueprint generate migrations for existing tables


Yes, Laravel Shift Blueprint can generate migrations for existing database tables. The key steps are:

1. Define the existing database schema in a YAML-based "draft" file:

yaml
models:
  User:
    username: string:128
    email: string:255
    password: string:255
    first_name: string:45
    last_name: string:45
    timezone: string:45
    location_id: id:location
    deleted_at: timestamp
    remember_token: string:255

2. Run the `blueprint:build` Artisan command to generate the migrations:


php artisan blueprint:build draft.yml

This will generate a migration file that matches the defined schema, including the table structure, columns, indexes, and foreign key constraints.

The generated migration will look something like this:

php
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('username', 128)->nullable()->index();
            $table->string('email', 255)->index();
            $table->string('password', 255);
            $table->string('first_name', 45)->nullable()->index();
            $table->string('last_name', 45)->index();
            $table->string('timezone', 45)->default('America/New_York');
            $table->unsignedInteger('location_id');
            $table->softDeletes();
            $table->string('remember_token', 255)->nullable();
            $table->timestamps();

            $table->foreign('location_id')->references('id')->on('locations')->onUpdate('cascade')->onDelete('cascade');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
};

This allows you to easily generate migrations for your existing database schema and integrate it into your Laravel application, without having to manually create the migration files.

Citations:
[1] https://blueprint.laravelshift.com
[2] https://laravel-news.com/laravel-shift-blueprint
[3] https://laravel-news.com/migration-generator-for-laravel
[4] https://github.com/laravel-shift/blueprint
[5] https://drawsql.app/docs/export-to-laravel-migrations