Saturday, August 27, 2016

Playing with PHP Artisan Migration Commands in Laravel 5.3 - Generating Migrations

We are ready to play with real php artisan migrate command now... because in previous part we have setup our laravel environment. Mmmm..... Here is the available command for migration in laravel 5.3 :
Documentation about migration can be seen here..
First.... Let's try migrate:install...
php artisan migrate:install
migrate:install will gonna make our migration table in our database. So after executing this, we will see a new table named migration as shown below :
What is the purpose of this migration table ? Well... This table will contain all of our migration file. So when we create a new migration file, this table will automatically update it's content when we execute php artisan migrate. We will gonna an example about this later.. :v
Next, Let's generate a new migration table. Type php artisan make:migration "your_migration_table_name", For example :
php artisan make:migration hisoka_first_migration
hisoka_first_migration is the name of our migration file...
After executing that command, we will see a new migration file inside of our database-migration folder like this :
As we can from above code, the migration file is not having a table name inside of method up(). So we will need to create it manually using laravel Schema. But there is also a way to create table automatically, from laravel documentation we can use flag like --table and --create. So let's see how these two flags work....
php artisan make:migration migration_1 --table=hisoka_table_1
Yaaps... there is our table name. So if we put --table flag, it will automatically generate our table name. Next let's see --create flag.
php artisan make:migration migration_2 --create=hisoka_table_1
From the code above, we can see that --create flag will also automatically create some column {id and timestamps} instead just creating our table name. 

Btw we still another flag that we can use, namely --path. This flag is used to specify the location of our migration file. In order to use this flag, let's create a new folder first in our laravel directory like this :
Then, run this command :
php artisan make:migration migration_3 --path=hisoka_migration_file


And the result is.....

Yaaaps... I think that's all for generating migrations in laravel 5.3.. See yaa in the next part... :)

No comments:

Post a Comment