#
Migrations
To create a table to hold our data, we'll use the Laravel migration system. Migrations let you expressively define modifications to your database, and easily share them with the rest of your team.
First, let's configure a database connection. You may configure all of your database connections from the app/config/database.php
file. By default, Laravel is configured to use MySQL, and you will need to supply connection credentials within the database configuration file. If you wish, you may change the driver
option to sqlite
and it will use the SQLite database included in the app/database
directory.
Next, to create the migration, we'll use the Artisan CLI
. From the root of your project, run the following from your terminal:
php artisan migrate:make create_users_table
Next, find the generated migration file in the app/database/migrations
folder. This file contains a class with two methods: up
and down
. In the up
method, you should make the desired changes to your database tables, and in the down
method you simply reverse them.
Let's define a migration that looks like this:
public function up()
{
Schema::create('users', function($table)
{
$table->increments('id');
$table->string('email')->unique();
$table->string('name');
$table->timestamps();
});
}
public function down()
{
Schema::drop('users');
}
Next, we can run our migrations from our terminal using the migrate
command. Simply execute this command from the root of your project:
php artisan migrate
If you wish to rollback a migration, you may issue the migrate:rollback
command. Now that we have a database table, let's start pulling some data!