Migrations
Originally I had attempted to include a guide to the Fluent Query Builder with this chapter,
but I now feel that the chapter has become too long, so I have decided to cover database set-up
and migrations only in this chapter. Get ready for a nice long explanation of Fluent in the next
chapter.
Migrations are one of my favourite Laravel features. I hate writing SQL, and the Schema class
allows me to create my tables easily without writing a single line of that foul “language”! Not
only that, but the Schema code also looks absolutely beautiful and reads like a book.
If you have not encountered migrations before, they are a way of describing changes to your
database in files so that different installations / development copies of your application are aware
of the current schema. Changes to the schema can also be reverted or ‘rolled back’. Migrations
can also be used to populate tables with example data (also known as seeding).
9.1 Database Setup
Head over to your application/config/database.php configuration file. If you have ever
installed a PHP application you will be familiar with this kind of file. You have your database
access credentials to hand don’t you? If not go dig them out right now!
Are you back yet? Great, let’s take a closer look.
Scroll down to the ‘connections’ array key, here you will see a number of options for a few
different type of databases. Fill in the connection parameters for your database of choice, I am
going to stick to good ol’ fashioned mySQL.
1 <?php
2
3 'mysql' => array(
4 'driver' => 'mysql',
5 'host' => 'localhost',
6 'database' => 'codefun',
7 'username' => 'root',
8 'password' => 'pandaseatbamboo',
9 'charset' => 'utf8',
10 'prefix' => '',
11 ),
There we go, now you will want to scroll up a little bit, and change the ‘default’ array key to
reflect the database you are using. For me it’s already on mySQL.
1 <?php
2
3 'default' => 'mysql',
Now we have our database configured, we need some tables to play with. Onwards to migrations!