In the previous tutorial we went through the thinking process behind database design and defined our data and how it is related to each other. We’ve learned how to break things apart in smaller pieces and how to relate data to each other.

In this tutorial we will do the actual coding and create our database, tables and fields. By the end of this tutorial you will have a database that you will use for your blog application!

Let’s get started!

Laravel has a very solid way of managing and interacting with databases, when you have your database connection set up in application/config/database.php , Laravel provides you with quite a few ways to create and drop tables, create and delete columns, insert and delete rows and much more.

Things we need to know while creating database for our blog, is Laravel’s Schema Builder and Migrations. Before we do any work with the database, let’s get an idea of how Laravel works with Databases. We will use the Schema Builder in our Migrations to tell our application what to do when we run the migrations through an Artisan command.

Migrations is like version control for your database, they allow you define actions that need to be taken when you run the migration or when you want to roll back changes in database structure. Those of you who have experience with Ruby on Rails will not be mistaken to assume that Laravel’s migrations work in a similar way to RoR’s.

NOTE: Before we run any migrations, we need to prepare the database by an artisan command that creates necessary table to keep track of our migrations:

php artisan migrate:install

After that, we can create migrations. I will show you how to do that after I explain about Laravel’s beautiful way of creating database schema, Schema Builder.

Schema Builder allows us to create database tables and insert rows quickly with Laravel (as an alternative you can do those things by hand for example in PHPMyAdmin but Laravel’s way is super simple). It uses clean and expressive syntax to make database operations happen.

For our blog, we need to create two migrations, one for posts and one for users. Each migration will have schemas for corresponding table and tell the Migration what to do when migration is run or rolled back.

Let’s go ahead and do that, have your terminal open and be in the folder with your application, type the following commands and hit enter after each one (remember you first have to run “php artisan migrate:install”) :

[sourcecode language="php"]php artisan migrate:make create_users
php artisan migrate:make create_posts[/sourcecode]

Now go and check your “application/migrations” folder, did you notice two new files there that start with a timestamp in their filename? Great, you’ve successfully created migrations for posts and users!

Open up those files (their filenames end with “create_users” and “create_posts”) in your favorite programming text editor (I use Sublime Text 2) and see their contents. We will need to edit these files and create database tables using Laravel’s Schema Builder. In “create_users” file, copy and paste this (you can see the code on GitHub ) :

[sourcecode language="php"]
<?php

class Create_Users {

/**
* Make changes to the database.
* Create the users table in the database and insert a record that store admin’s credentials
*
* @return void
*/
public function up()
{
Schema::create(‘users’, function($table) {
$table->increments(‘id’);
$table->string(‘username’, 64);
$table->string(‘password’, 64);
$table->string(‘name’, 128);
$table->timestamps();
});

DB::table(‘users’)->insert(array(
‘username’ => ‘admin’,
‘password’ => Hash::make(‘password’),
‘name’ => ‘Admin’
));
}

/**
* Revert the changes to the database.
* In this case we just drop the table
* @return void
*/
public function down()
{
Schema::drop(‘users’);
}

}
[/sourcecode]

Now let’s edit the “create_posts” file and copy/paste the contents from the following:
[sourcecode language="php"]
<?php

class Create_Posts {

/**
* Make changes to the database.
* Create the posts table in the database
*
* @return void
*/
public function up()
{
Schema::create(‘posts’, function($table) {
$table->increments(‘id’);
$table->string(‘post_title’, 255);
$table->text(‘post_body’);
$table->integer(‘post_author’);
$table->timestamps();
});
}

/**
* Revert the changes to the database.
* In this case we just drop the table
* @return void
*/
public function down()
{
Schema::drop(‘posts’);
}

}
[/sourcecode]

Awesome !
Now we need to run our migrations to actually get our tables created in the database, run the following Artisan commands :
[sourcecode language="php"]php artisan migrate:install
php artisan migrate[/sourcecode]

That’s it with the database, we have our tables created!

In the next part of the tutorial we will make the User and Post models and creates our routes!

Check out the next part here: http://maxoffsky.com/code-blog/tutorial-creating-a-blog-in-laravel-from-scratch-part-5-create-models-and-routes/

The following two tabs change content below.

Maks Surguy

Full stack web developer, speaker and writer.
Maks is young and energetic breakdancer turned into web developer who lives in Seattle area with his wife. He is well-versed in three languages (English, Russian, Ukrainian) and a dozen of programming languages.