Laravel Tutorial: Database Migrations
Table of Contents
Introduction
Laravel provides an easy and powerful way to create database structure using code. This method allows you to define your database tables and columns in text files, which makes it easy to edit and develop as needed. Laravel also provides a built-in rollback method which allows you to revert any database structure changes as needed. Learn more about Laravel database migrations and how to use them.
Requirements
- A 1&1 Cloud Server with Laravel and PHP 7.0+ installed.
To install Laravel, follow the instructions in our article Install the Laravel PHP Framework on Ubuntu 16.04.
Overview
For this project we will be creating an example website for restaurant reviews. Visitors will be able to submit restaurant reviews using a PHP form, and view existing restaurant reviews which are stored in a MySQL database.
Create the Database
To begin, you will need to create a database for your project, and a database user for Laravel to use. Log in to the MySQL client:
mysql -u root -p
Create a database for the reviews:
CREATE DATABASE reviews_db;
The following command will create a user review_site
with password JxSLRkdutW
and grant the user access to the reviews_db
database:
GRANT ALL ON reviews_db.* to review_site@localhost IDENTIFIED BY 'JxSLRkdutW';
Create the Laravel Project
Use su
to switch to your Laravel user:
su - jdoe
Go to your web directory:
cd /var/www/html
Create the Laravel project:
laravel new reviews
This will create the reviews
directory. Move into this directory:
cd reviews
Next, we will set the MySQL credentials in the Laravel environment file. Open the .env
file for editing:
nano .env
Find the section which reads:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
Change the DB_DATABASE
, DB_USERNAME
, and DB_PASSWORD
lines to include your project's MySQL database name, username, and password:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=reviews_db
DB_USERNAME=review_site
DB_PASSWORD=JxSLRkdutW
Save and exit the file.
Create and Run the Migration
Laravel migrations act as a version control system for your database. This allows your team to easily collaborate and edit database design.
Use the make:migration
Artisan command to create a migration named create_reviews_table
. We will also include the --create
flag to create the table user_reviews
:
php artisan make:migration create_reviews_table --create=user_reviews
The new migration will be located in the database/migrations
directory, and will have a name which includes the timestamp:
jdoe@localhost:/var/www/html/reviews$ ll database/migrations
total 20
drwxrwxr-x 2 jdoe jdoe 4096 Oct 26 16:16 ./
drwxrwxr-x 5 jdoe jdoe 4096 Oct 26 16:15 ../
-rw-rw-r-- 1 jdoe jdoe 746 Oct 26 16:15 2014_10_12_000000_create_users_table.php
-rw-rw-r-- 1 jdoe jdoe 683 Oct 26 16:15 2014_10_12_100000_create_password_resets_table.php
-rw-rw-r-- 1 jdoe jdoe 602 Oct 26 16:16 2017_10_26_161616_create_reviews_table.php
Each migration class includes two methods: up()
and down()
. The up()
method will add new architecture (tables, columns, indexes, etc.) to the database. The down()
method will reverse operations which have been performed by the up()
method.
Open the database migration file for editing:
nano database/migrations/2017_10_26_161616_create_reviews_table.php
Each migration automatically includes the auto-incrementing id
column, and the timestamps()
columns. We will be adding three columns to the table:
reviewer_name
: A string with a maximum of 100 characters.star_rating
: An integer.details
: A text field.
For a full list of column types, see the official Laravel documentation on migrations.
Locate the up()
method section in the migration file:
public function up()
{
Schema::create('user_reviews', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
});
}
Add the new column definitions so that this section reads:
public function up()
{
Schema::create('user_reviews', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->string('reviewer_name', 100);
$table->integer('star_rating');
$table->text('details');
});
}
Save and exit the file. Then run the migration:
php artisan migrate
If you need to reverse (roll back) a migration, use the command:
php artisan migrate:rollback