PHP

Migration Generator Package for Laravel.

Migration Generator for Laravel.

Migration Generator for Laravel is a package by Bennett Treptow to generate migrations from existing database structures.

If you are looking to convert your existing application into Laravel or want to use existing database for Laravel project, then this package could be helpful.

Following is the table definition for which we want to create migration class as example.

CREATE TABLE `users` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `username` varchar(128) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `password` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `first_name` varchar(45) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `last_name` varchar(45) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `timezone` varchar(45) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'America/New_York',
  `location_id` int(10) unsigned NOT NULL,
  `deleted_at` timestamp NULL DEFAULT NULL,
  `remember_token` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `users_username_index` (`username`),
  KEY `users_first_name_index` (`first_name`),
  KEY `users_last_name_index` (`last_name`),
  KEY `users_email_index` (`email`),
  KEY `fk_users_location_id_index` (`location_id`)
  CONSTRAINT `users_location_id_foreign` FOREIGN KEY (`location_id`) REFERENCES `locations` (`id`) ON UPDATE CASCADE ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

By using this package, you can run following command to generate blueprint class against the above table definition.

php artisan generate:migrations

Users table class will look like as following as per above mentioned table definition.

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

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('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', 'users_location_id_foreign')->references('id')->on('locations')->onUpdate('cascade')->onDelete('cascade');
        });
    }

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

The package also has some table and view migration stubs and configuration settings. Such as, following configuration defines the filename patterns used to generate table schema.

return [
    'table_naming_scheme' => '[Timestamp]_create_[TableName]_table.php',
    // ...
];

You can get more information about this package from GitHub repo.

adam

Recent Posts

How to reset WSL 2 user’s password?

You can easily reset WSL 2 users' password, by just following the following steps. Open…

2 months ago

DreamHost Web Hosting

DreamHost a web hosting company, founded in 1997. It is offering sort of hosting services,…

10 months ago

How to add submenu or menu in any specific menu option in WordPress programmatically?

Menus in WordPress are highly versatile and can be easily modified to change in your…

11 months ago

Laravel 8 error target class controller does not exist.

Laravel is famous and robust PHP framework, widely used in different type of projects. While…

1 year ago

Define Private Methods/Functions in Python Class.

Python is very powerful and famous language, which allow us to write code as per…

2 years ago

Working with dates in PHP.

In this article, I am going to show you how we can get specific dates…

2 years ago