Tweet about this on TwitterShare on FacebookShare on Google+Share on StumbleUponShare on TumblrShare on RedditPin on PinterestEmail this to someone
We will try to implement simplest end-to-end web application, at the same time will try to cover most concepts and best approach to implement it. So our application is very simple one. It has just one database table category where we save id, code, name, created time, and modified time. UI will have search panel where you can search categories, add edit delete a category. First thing we have to do now is create the database.

Step 1 – Create the Database

Open the terminal window and connect to mysql database using following command.

mysql –u root –h localhost –p

Then create a new database

mysql> create database lbc;

Create MqSql Database

Create MqSql Database

Step 2 – Configure Laravel DB Settings

A sip of Coffee with Laravel and Backbone - Book

 

If we need we can create the category table here using MySql command prompt, instead will use Laravel artisan migration tool to create the model and then generate the table. Before doing that we need to set our database settings in Laravel config file. Go to following folder lbc/application/config/. Open database.php file in your editor and find ‘connections’ => around line 60. Then change mysql connection properties according to your environment. If you have setup mysql root password set it against ‘password’, if not you don’t need to change it, also set the ‘database’ to ‘lbc’.

 

Step 3 – Use Laravel Astisan Migrations

Then we need to initialize our database to support Laravel artisan migrations. If you don’t know what migration is, it is simply a way of creating database schema using programming language (here it is PHP) and also whenever needed downgrade the changes made by previous migrations without directly manipulating the database schema using DDL. You can find good documentation about this in Laravel website. In order to keep track of what changes we made to the database schema over the time, it needs to store the changes made, so it needs special table in the database. We can create this table by following command. First go to ‘lbc’ folder in the terminal and issue following command.

php artisan migrate:install

You will get success message, and if you need to see what changes made by it, login to mysql with database lbc.

mysql –u root –h localhost –D lbc –p

Then type

mysql> show tables;

You will now see a new table called ‘laravel_migrations’ has been created. This is the table which keeps migration history.

Now we need to create migration script to create our category table. While you at ‘lbc’ folder execute following command.

php artisan migrate:make create_category

Create the Table Generation Code

Create the Table Generation Code

This will create skeleton script for migration under ‘application/migrations’ folder. Open this file in your editor, and write following code inside up() method.

What this does is, when we ask artisan tool to migrate, it will execute any pending script like this one we created, and it executes up() method on the database, so we have to tell it what we need to do when this migration script is applied. Laravel has provided Schema class with bunch of static methods that we can use to create database schema easily.

Line 3: We instruct to create new table with the name ‘tbl_category’ and also will give more information about the structure of it inside the anonymous function.

Line 4: We need auto increment id column of type int and with the name ‘id

Line 5: We need varchar(20) column with the name ‘code’ (not null)

Line 6: We need varchar(100) column with the name ‘name’ (not null)

Line 7: Finally we need two more column to keep created time and updated time of this row.

Now in the down method we need to tell what to do when database downgrade happens, (roll back to previous state) so it is obvious we need to drop this table. so in down() method write following.

Complete code would be as follows.
Now save the file and go to terminal command line (‘lbc’) folder and execute this.

php artisan migrate

You will get successful message as below.

Artisan Migration Success Message

Artisan Migration Success Message

Then go to mysql and see whether your table is created with given format.

Category Table Definition

Category Table Definition

 

Posts of this series
Laravel, Backbone.js + Coffeescript Tutorial Part I – Introduction
Laravel, Backbone.js + Coffeescript Tutorial Part II – Setup
Laravel, Backbone.js + Coffeescript Tutorial Part III – Install Laravel
Laravel, Backbone.js + Coffeescript Tutorial Part IV – Database
Laravel, Backbone.js + Coffeescript Tutorial Part V – Data model
Laravel, Backbone.js + Coffeescript Tutorial Part VI – REST API
Laravel, Backbone.js + Coffeescript Tutorial Part VII – REST API
Laravel, Backbone.js + Coffeescript Tutorial Part VIII – Views
Laravel, Backbone.js + Coffeescript Tutorial Part IX – Coffeescript
Laravel, Backbone.js + Coffeescript Tutorial Part X – Backbone
Laravel, Backbone.js + Coffeescript Tutorial Part XI – Backbone
Laravel, Backbone.js + Coffeescript Tutorial Part XII – Backbone
Laravel, Backbone.js + Coffeescript Tutorial Part XIII – Backbone

Tweet about this on TwitterShare on FacebookShare on Google+Share on StumbleUponShare on TumblrShare on RedditPin on PinterestEmail this to someone