Introducing Laravel, part 2

In the first part of this introductory mini series we looked at simple routes and views and now we’ll look at how to work with controllers and models , how these two fit in the framework and how to use them.


In the first part of this article I showed you how to link routes to views, but as you might have noticed we still haven’t added any logic and that is where controllers come into play.

Controllers contain public methods that are known as actions and a simple controller that calls a view looks something like this:

class UserController extends BaseController{
public function showProfile() {
return View::make('profile');

This is as simple as we can get with controllers; we create a UserController class that will contain all the functions that are needed in our application when it comes to users. Inside it we create a function called showProfile that returns the profile view.

This is complete, but in order for it to redirect the user to the right place, we need to link it to the routes. So we’ll use controller routing and to link that controller we just made to a route we need :

Route::get('profile', '[email protected]');

To link a URI to a controller we still used the Route::get() method and the first parameter still works the same way , it’s with the second that things vary a little.

In the previous article we had a function there, but since our logic is in the controller, that function is no longer needed; we just need to point to the action we want. So, for the second parameter we add the class name and an @ sign and lastly the name of the action we wish to route.

Eloquent ORM

The Eloquent ORM is a simple and beautiful ActiveRecord implementation that helps you work with your database. For each database we use we should have a corresponding Model to interact with that table.

class Post extends Eloquent {}

One thing we usually do not need to do is tell Eloquent which table to use, Eloquent will take the name of the class we use, make it lowercase and plural and that will be used as the table name (unless another name is specified). In this class Eloquent will assume I am using the posts table because my class name is Post.

If I want to use a different table I can specify it as follows:

class Post extends Eloquent {
protected $table = 'articles';

Eloquent will also assume that each table you create has a primary column named id. Once this is created you are ready to start retrieving and creating records in your table. You will also need updated_at and created_at columns because this the default approach taken by Laravel. (To avoid this you’ll need to set the $timestamps property on your model to false.)

Final Thoughts

This completes our quick introduction to Laravel, this introduction was just a little taste to get you warmed up, and exited about learning this amazing framework.

I hope these two articles showed you the benefits in using Laravel and how great your code ends up looking when you use it.

Sara Vieira is a freelance Web Designer and Developer with a passion for HTML5/CSS3 and jQuery. You can follow her on twitter or check out her website. More articles by Sara Vieira
  • alexsawallich

    Nice two articles. Of course we could check out the Docs at the Laravel-Website. However I think it would be cool for new users to see how a model works. You just created one, but how to I fetch records within my action and how do I inject them into my view?

    I feel this information is missing in here.

  • Janek Kowalski

    Just a little taste that dont warm up.

  • Thanks for your sharing. It is really important useful information for my research

  • jomyfresh14

    hdUO Recently i have been lacking in $$ and debts were killing me from everywhere. That was RIGHT UNTIL i decided to make money on the INTERNET. I landed on surveymoneymaker p net, and started doing surveys for straight cash, and surely I have been far more able to get around financialy!! i’m glad, that i did this!!! With all the financial stress these years, I really hope all of you will give it a chance. – 3cPQ

Home CSS Deals HTML HTML5 Java JavaScript jQuery Miscellaneous Mobile MySQL News PHP Resources Security Snippet Tools Tutorial Web Development Web Services WordPress