laravel4 Eloquent ORM Model, Blade View & Simple Controller

So far we have completed four blog posts for the series on development of thisdayinbangladesh, a simple laravel4 live application. Those are following:

1. Creating virtual host
2. Installing laravel4
3. laravel4 migrations & seeding
4. laravel4 routing

In this post we will learn, how we can have Object Relational Mapping for our data model using Eloquent ORM that is built in with laravel4. Also we will render our view using Blade templating system. Finally we will create few very simple controllers that will do our basic tasks.

Model:

Laravel ships with a superb ORM: Eloquent. If you have used the Ruby on Rails framework, you will find Eloquent familiar, as it follows the ActiveRecord ORM style of database interaction.

At this current stage (state before merging with any other branch on github) of our application we need such a relationship among our database tables (days, facts, types, fact_type) so that;

i. A Day has many Facts: That means there would be a relation from “days” table to “facts” table that is One to Many. N. B. Currently “days” table contains 366 days in separate rows.
ii. A Fact belongs to a Day: That means in a certain case, one Or more Fact(s) will belong to a single Day. That is a reverse of One to Many relation from “facts” table to “days” table.
iii. A Type has many Facts & a Fact has many Types: For example a fact type named “politics” could have several facts for a certain day. On the other hand a fact titled “Independence day of Bangladesh” could have several types like “politics”, “good”, “revolutionary” etc. So, there would be a Many To Many relationship between “types” table & “facts” table.

Lets create our first Model called “Day.php” for our “days” table inside “app/models” directory with the following code.


First check the comments on line 5-10. Hope you’ve understood what is said there.
Note that we did not tell Eloquent which table to use for our Day model.The lower-case, plural name of the class will be used as the table name unless another name is explicitly specified. Then by the definition of the function “facts()” we are defining a One to Many relationship between Day & Fact model.

Now lets create another Model named “Fact.php” for the “facts” table in same directory with the following code.

Here we are defining a reverse of One to Many relationship between Fact model & Day model by the “day()” function.

As we discussed above that we have to manage “fact types” for our application. “type” table will contain some popular fact types like “birth”, “death”, “sport”, “politics” etc in it. Lets create another Model named “Type.php” for the “types” table with following code.

Since, a Type belongs to many Facts, we created a, Many to Many relationship from Type model to Fact model by the “facts()” method. Now we will have to put a same kind of relationship on the Fact model. So, update the “Fact.php” model like the following code.

Here on the bottom, we defined the relationship of Fact inside the “types()” method that it belongs to many types.

Finally we will obviously have to store Type ID & Fact ID relation mapping in a pivot table. We will name it “fact_type” in our database. We maintained the convention that is needed by this ORM & laravel that is; singular name of the alphabetically first table AND singular name of the alphabetically second table joining with an underscore. So, we don’t need to define a custom pivot table name to the ORM by passing any additional parameter while creating/defining relationship.
So, lets make another model for this “fact_type” table named “FactType.php” with nothing special code in it.

Views:

One method of using templates in Laravel is via controller layouts. By specifying the layout property on the controller, the view specified will be created for you and will be the assumed response that should be returned from actions.

But in our developing app, we are going to use Blade Templating. Its a great templating engine that comes with laravel4. Unlike controller layouts, Blade is driven by template inheritance and sections. All Blade templates should use the “.blade.php” extension.

First of all, we need a master layout which would contain all the sub views (header logo, navigations, sidebar, top section, body, footer etc) in a sectional structure in it. Lets create our master view named “layoutfront.blade.php” inside “app/views” directory.

Though it looks so much code here but take a deeper look and you will get it simple. This is a one page template that was made using the UI elements of Flat UI design kit. We first requested the CSS & Js assets that needed for front end view, on top of the HTML script. N. B. Don’t bother why I placed those such a static way. Its up to you later to make this placement in a more beautiful & effective way.
On the very bottom of the template we used few lines of jQuery so that if user choose a pair of date and month from a select box then it redirects to a specific URL that contains a route along with date and month as parameters on it.
Then, on the line 99 we defined an area in which a sub view will be called up while rendering the full view. That sub view was named “dateandtalkofthisday.blade.php” inside “app/views” directory and contains the following code.

You can see a pretty simple implementation of Blade Templating like we defined the section name on the very top of the file and finished it on the very bottom. On line 5, 6, 15, 19 we are using some variables that holds values on those and populating data in convention of Blade Templating system.
You can have a look to other sub views (Needed from inside the master view (layoutfront.blade.php on line 122, 123 of it) inside “app/views” directory if you cloned/downloaded the full github repository.

Controller:

So far our data model & their relationships mapping are done. Also we did split a view file and made some sections. Finally we used Blade’s looping, conditions & other syntaxes to render the view by some data variables.
Now we will create the Controllers that will act at very first time when a URL is requested & a rout become performed. Those controllers will make data ready by using the Models & will provide to the view files.
If you already checked out the post about routing for this application, then you should see, the second Route we used to access the RESTful “FactController.php” is called when user requests the root URL of this application via first Route. So, lets create the “FactController.php” RESTful controller.

Laravel allows you to easily define a single route to handle every action in a controller using simple, REST naming conventions. Just add methods to your controller, prefixed with the HTTP verb they respond to.

We named the methods in above quoted HTTP REST naming conventions. So, actually our method name is something like “Index” that returns a view with some data but the method named with “GET” keyword prefixed in it. This is the convention of a RESTful Controller in laravel4.
Then on line 9, 10 we are retrieving data using Eloquent ORM. Just check line 10 where we are getting all the facts of a specific day (that is today) using a single line. Without even using query builder. Thats the awesomeness of using ORM (Though you can use query builder along with ORM if needed in special cases). After that, all are known to you till line 21.
In line 22, we are returning the View named “eventlisting” which will contain data of allFacts & thisDay array in “facts” & “today” variables. You can check here about binding data with views in laravel.
The code of the “eventlisting.blade.php” file is following.

Which is extending the master layout “layoutfront”, including the “dateandtalkofthisday” sub view & holding the data variables that was sent by the Fact controller.

Download/Fork Download/Fork on Github
Watch Demo Live/Demo Application

And thats all! Stay tuned by subscribing via email or by following as WP blogger.

2 thoughts on “laravel4 Eloquent ORM Model, Blade View & Simple Controller”

Comments are closed.