Tutorial: Creating a Blog in Laravel from scratch – part 4 – Create tables and migrations

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: https://maxoffsky.com/code-blog/tutorial-creating-a-blog-in-laravel-from-scratch-part-5-create-models-and-routes/

Liked it? Take a second to support Maks Surguy on Patreon!
Become a patron at Patreon!

You may also like

22 comments

  • George Petsagourakis August 5, 2012  

    Good job on these tutorials, thanks a lot for this. Eagerly awaiting for the next one!

  • Sergio Palomeque October 20, 2012  

    If a Posts belongs to a User, why no foreign key in the table Posts?

  • Sergio Palomeque October 20, 2012  

    I’m sorry, I understand now. Thanks for your articles, very helpful!

  • P Roach October 25, 2012  

    Using Postgresql 9.2: Migration not working for me as timestamps() creates NOT NULL columns, but your insert does not provide data for them in the insert? Thanks for any advise.

  • Maks Surguy October 27, 2012  

    Where do you have the problem, when creating the posts or when running migrations?

  • P Roach November 16, 2012  

    Running migrations when the insert step for the admin user in your code above.

  • Cam Tyler December 13, 2012  

    when running php artisan migrate I get “Call to undefined function table()” from the create_users migration.

  • Maks Surguy December 15, 2012  

    Can you post your migration files here?

  • sanny January 28, 2013  

    can u explain me where do i type artisan command briefly .When i type in cmd ,it says ‘php is not internal or external commands ….. ‘

  • sanny January 28, 2013  

    hi , i really don’t konw where to start using artisan .. when i googled it , i’ve to add environmental variable ,, pls can u explain me how can i

  • Maks Surguy January 31, 2013  

    Do you use Windows or Mac?

  • beno April 6, 2013  

    your script is missing some script so it makes some error stupid!

  • Maks Surguy April 7, 2013  

    where?

  • R Proac May 3, 2013  

    I couldn’t run php artisan migrate:install because it gave me “could not find drive”. But I did it by writing: /opt/lampp/bin/php-5.4.7 /opt/lampp/htdocs/blog_laravel artisan migrate:install. And I could create the create_users and create_posts classes but when I try to run php artisan migrate it gave me the same, so I do the same (with the full addresses) with no errors, but nothing happens on the database. I will be grateful for your answer, and sorry for my English

  • Maks Surguy May 5, 2013  

    you are on linux?

Leave a comment