|

Laravel Migration Generator

Laravel Migration Generator

In this tutorial, we’ll work with a Laravel Migration Generator. When generating our migrations, it can sometimes be a bit confusing as to the correct way to add columns or fields to our tables. There are a large amount of methods to use within the Schema and Blueprint classes to wrap our heads around. Many times, you might even have a database in place, but you would like to move this instance to another host, or simply have a blueprint of the database in a set of migration files. Wouldn’t it be great if we had a way to handle doing this for us automatically? In fact we do now thanks to the work of barryvdh and now Xethron. Let’s see how to put this in action.


Configure Your Laravel Environment

  • Step 1. The first thing we will do for this is to install the needed dependencies into our composer.json file. Per the instructions at the Xethron github page, we can set up our composer.json file to pull in what we need. There are a few snippets at the prior link, here is the entirety of the composer.json file used in this testing demo.

  • Step 2. Next up, we need to simply run a composer update. At the terminal, simply type composer update then hit enter.
  • Step 3. Add the following to the providers array in app.php

  • Step 4. Configure the .env file to point to the local database that has the tables you want to create migrations for. In this example, we’ll point Laravel at a WordPress database for a common example.
  • Step 5. Run the migrate commands and prosper!
vagrant@homestead:~$ php artisan migrate:generate
Using connection: mysql

Generating migrations for: wp_commentmeta, wp_comments, wp_links, wp_options, wp_postmeta, wp_posts, wp_term_relationships, wp_term_taxonomy, wp_terms
, wp_usermeta, wp_users
Do you want to log these migrations in the migrations table? [Y/n] Y
Migration table created successfully.
Next Batch Number is: 1. We recommend using Batch Number 0 so that it becomes the “first” migration [Default: 0]
Setting up Tables and Index Migrations
Created: … create_wp_commentmeta_table.php
Created: … create_wp_comments_table.php
Created: … create_wp_links_table.php
Created: … create_wp_options_table.php
Created: … create_wp_postmeta_table.php
Created: … create_wp_posts_table.php
Created: … create_wp_term_relationships_table.php
Created: … create_wp_term_taxonomy_table.php
Created: … create_wp_terms_table.php
Created: … create_wp_usermeta_table.php
Created: … create_wp_users_table.php

Setting up Foreign Key Migrations

Finished!

Boom! Just like that, we have all our migrations finished. It’s a bit of a nonsensical example, but just think of the use cases it has. The first one that comes to mind is, say you have an app built on another framework. There is an existing database for which you would like to quickly create migrations for, so that you can then move over to writing the Laravel code. With this tool, you can do just that. Just for reference, here is the output of the migration source code that was generated for us in this example.

create_wp_commentmeta_table.php source

create_wp_comments_table.php source

create_wp_links_table.php source

create_wp_options_table.php source

create_wp_postmeta_table.php source

create_wp_posts_table.php source

create_wp_term_relationships_table.php source

create_wp_term_taxonomy_table.php source

create_wp_terms_table.php source

create_wp_usermeta_table.php source

create_wp_users_table.php source

Laravel Migration Generator Conclusion

As we can see, this is a really cool Laravel Migration Generator the community created. It’s a great way to reverse engineer existing databases as well as to help learn about how to create migrations. The files above give us a good overview of the various methods we can use off of the $table instance in our migrations files.