Create a new database table using migrate

Leave a comment

August 6, 2016 by movefirstblog

Laravel makes the creation and management of database tables easy with the provision of the migrate command.

Before we can add our new table in Laravel we must ensure that the database into which the table is going to inserted has been created.

The easiest way to do this is to use phpMyAdmin.  If you need help in using phpMyAdmin to do this have a look at this site: http://webvaultwiki.com.au/(S(iungfwenvz2f5c55pioeinil))/Default.aspx?Page=Create-Mysql-Database-User-Phpmyadmin&NS=&AspxAutoDetectCookieSupport=1

The syntax for the command to create the new table is:

php artisan make:migration [name_of_migration] --create=[table_name]

where:

[name_of_migration] is the name you wish to give to your migration.

Good practice dictates that you should name your migration such that it clearly shows what it does.

For example, 2016_08_08_065832_create_products_table clearly shows that the migration is going to create a new table named products.

and

[table_name] is, unsurprisingly, the name you wish to give to your new table.

The new migration will be created in the database/migrations folder within your project with a name in the format yyyy_mm_dd_xxxxx_name_of_migration.php.

In our example above, our migrate command would be:

php artisan make:migration create_products_table --create=products

We then execute this (and any other migration scripts) with the code:

php artisan migrate

We now need to add fields to the newly created database.  To see how we do this take a look at Adding fields to a new database using migrate.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: