Advertisement
  1. Code
  2. PHP
  3. Laravel

Build Web Apps from Scratch with Laravel - The Eloquent ORM

Scroll to top

In this Nettuts+ mini-series, we’ll build a web application from scratch, while diving into a great new PHP framework that’s rapidly picking up steam, called Laravel.

In this lesson, we'll be working on an integral part of any web application: the Models. Along the way, we'll learn about Laravel’s amazing ORM implementation: Eloquent.


Review

Welcome back to our Web Applications from Scratch with Laravel series! In the first tutorial of the series, we learned a lot about Laravel and its philosophy:

  • What Laravel is
  • What makes Laravel different from other PHP frameworks
  • Where to download Laravel
  • How to setup Laravel
  • How Laravel's Routing system works
  • Some other features in Laravel's Routing system
  • How to create your first Laravel Controller
  • Some additional features with Laravel's Controllers
  • How to create your first Laravel View
  • How to use Laravel's Blade Templating Engine

If you haven't read it yet, you should take a look at the previous tutorial and give it a read - this will make it easier to understand the philosophy behind Laravel and most of what we discuss in this tutorial.

In this second part of the Laravel series, we'll be building a crucial part of our test web application, Instapics, which is the Model implementation. Without furhter ado, let's get started!


What are “Models”?

I've already talked a bit about what Models are in one of my previous articles, Zend Framework from Scratch - Models and Integrating Doctrine ORM, so to avoid repeating myself, I'll write the gist of what I wrote before here. Feel free to refer to the other tutorial and read more about what Models are there.

Summary:

  • Models are representatives of the Database, and should be where all the business logic of an application resides
  • Controllers communicate with Models and ask them to retrieve information they need
  • This information is then passed by a Controller to the View and is rendered
  • It's very rare that a Model directly interacts with a View, but sometimes it may happen when necessary
  • Models can talk with other Models and aren't self-contained. They have relationships that intertwine with each other
  • These relationships make it easier and quicker for a Controller to get information, since it doesn't have to interact with different Models - the Models can do that themselves

Models in Laravel, or in most frameworks, are developed the same way. The difference is that Laravel give us an easy way of building these models, by providing us with general-purpose methods that most models would need - the Eloquent ORM.


The Eloquent ORM

An ORM is an object-relational mapper, and Laravel has one that you will absolutely love! It is named "Eloquent," because it allows you to work with your database objects and relationships using an eloquent and expressive syntax.

The Eloquent ORM is Laravel's built-in ORM implementation. In my opinion, it's one of the best ORM implementations I've seen so far - rivaling even Doctrine ORM. It's incredibly elegant, making use of industry-standard conventions to lessen configuration.

Conventions

For example, using an Eloquent model assumes that the table the model is representing has an id field. The id is the primary key for any record, and is used by most of Eloquent's methods.

Another thing that Eloquent correctly assumes is that your table name is the plural form of your model. For example, your User model will reference the users table. As this might not always be the standard for some, Laravel provides a way to override this: simply use the $table flag:

1
class User extends Eloquent {
2
    public static $table = 'my_users';
3
}

This will instruct Laravel not to use the convention and instead use the specified table.

Lastly, Laravel can also automate the creation and updating of timestamps for us. To do so, add a created_at and/or updated_at column in the table, and set the $timestamp flag in the model:

1
class User extends Eloquent {
2
    public static $timestamps = true;
3
}

Eloquent will see the flag, and automatically set the created_at field on creation, and update the updated_at field each time that a record is updated. Pretty cool, huh?

Quick Retrieval

Retrieving records is a snap with Eloquent's retrieval methods. For example, you need to find a specific user record? Just do:

1
$user = User::find($user_id);

This returns a User model that you can do operations on! Need to use conditionals? Let's imagine that you want to retrieve a user by email address. To accomplish this task, you might do something like:

1
$user = User::where('email', '=', $email)->first();

Alternatively, you could use Laravel's dynamic methods:

1
$user = User::where_email($email)->first();

Easy Inserts & Updates

Inserting and updating models using Eloquent can be accomplished in three steps.

  • Step 1 - Get/Create the model.

    1
            $user = new User();
    
    2
            //or get an existing user
    
    
    3
            $user = User::get($user_id);
    
  • Step 2 - Set the data

    1
            $user->email = 'nikko@instapics.com';
    
    2
            $user->password = 'test1234';
    
  • Step 3 - Save

    1
            $user->save();
    
  • Done!

And finally, Defining Relationships.

Eloquent makes the process of defining relationships and retrieving related models simple and intuitive.

Damn right it does! Eloquent supports three types of relationships:

  1. One-to-One
  2. One-to-Many
  3. Many-to-Many

To define a relationship between models, you'll need to create a method in both models that "describes" their relationships. For example, let's say a User has_one User_Profile. You can do that by defining a user_profile method in the User model:

1
class User extends Eloquent {
2
    public function user_profile()
3
    {
4
        return $this->has_one('User_Profile');
5
    }
6
}

Because User is our "dominant" model here (i.e. a user has a profile, and not a profile has a user), we define that a User_Profile belongs_to a User:

1
class User_Profile extends Eloquent {
2
    public function user()
3
    {
4
        return $this->belongs_to('User');
5
    }
6
}

Once we've defined these relationship, we can then do:

1
/*

2
  Get the User_Profile object of a User

3
  This executes two SQL queries:

4
  

5
  SELECT * FROM `users` WHERE `id` = $user_id

6
  SELECT * FROM `user_profiles` WHERE `user_id` = $user_id

7
*/
8
$user = User::find($user_id);
9
$user_profile = $user->user_profile;
10
11
/*

12
  We can also do it the other way around

13
*/
14
$user_profile = User_Profile::where('user_id', '=', $user_id)->first();
15
$user = $user_profile->user;

One thing worth noting here is another convention: Eloquent assumes that the foreign key used in User_Profile is the referenced table's name + _id. Again, if you want to change this behaviour, you can override it:

1
class User extends Eloquent {
2
    public function user_profile()
3
    {
4
        return $this->has_one('User_Profile', 'user_profile_user_id');
5
    }
6
}

Let's say that we want to define the relationship between a User and his Photo uploads. This is a One-to-Many relationship, unlike the User-to-User Profile relationship which was One-to-One. We know that one User has_many Photo uploads, so:

1
class User extends Eloquent {
2
    public function photos()
3
    {
4
        return $this->has_many('Photo');
5
    }
6
}
7
...
8
...
9
...
10
class Photo extends Eloquent {
11
    public function user()
12
    {
13
        return $this->belongs_to('User');
14
    }
15
}

The main difference here with has_one is that the function we'll use to retrieve a User's photos will now return an array of Photo objects. So, if we wanted to fetch all of a User's photos, we could do:

1
$photos = User::find($user_id)->photos;
2
foreach($photos as $photo) {
3
    ...
4
    ...
5
    ...
6
}

Nope, referring to photos as a property isn't a typo. Laravel gives us this nice bit of sugar. We could also do:

1
$photos = User::find($user_id)->photos()->get();

Mant-to-Many Relationships

This one is a bit tricky, but once implemented, it makes it easy to handle Many-to-Many relationships between models. Let's imagine, for example, that you, again, have a User model, and each of these Users can have multiple Groups. A Group can also have multiple Users. We'll use three tables to represent these particular relationships:

  • Users - table where all our users are
  • Groups - table where all our groups are
  • Group User - table which lists down all users in a group

The table structure convention Eloquent will look for will be something like this:

  • users
    • id
    • ...other columns
  • groups
    • id
    • ...other columns
  • group_user
    • id
    • user_id
    • group_id
    • ...other columns

One other convention to note here is that the intermediate table, group_user, is the singular names of the two tables that it is connecting, arranged alphabetically with an underscore. Like always, we're free to override this.

Here's how the code will look inside each of the models for these three tables:

1
class User extends Eloquent {
2
    public function groups()
3
    {
4
        //if we wanted to override the default naming convention

5
        //for the intermediate table, we can do it like so:

6
        //return $this->has_many_and_belongs_to('Group', 'group_listings');

7
        
8
        return $this->has_many_and_belongs_to('Group');
9
    }
10
}
11
...
12
...
13
...
14
class Group extends Eloquent {
15
    public function users()
16
    {
17
        //if we wanted to override the default naming convention

18
        //for the intermediate table, we can do it like so:

19
        //return $this->has_many_and_belongs_to('User', 'group_listings');

20
        
21
        return $this->has_many_and_belongs_to('User');
22
    }
23
}
24
...
25
...
26
...
27
class Group_User extends Eloquent {
28
    public function group()
29
    {
30
        return $this->has_one('Group');
31
    }
32
    
33
    public function user()
34
    {
35
        return $this->has_one('User');
36
    }
37
}

With this in place, we can then take advantage of Eloquent's relationship functions:

1
//Get a user's groups

2
$groups = User::find($user_id)->groups;
3
4
//Get all users in a group

5
$users = Group::find($group_id)->users;

Step 1 - Creating the Instapics Database

Instapics

Continuing with our web application, Instapics, let's start off by creating the database of our application. To do so, let's write down the desired functionalities of the application:

  • Users can login and register for an account
  • Users can follow other users to see photos they have uploaded
  • Users can upload their own photo and apply a filter to it
  • Users can comment and like photos

From this, we can deduce the database tables we'll need:

  • users

    • id (One-to-One with user_profiles.user_id, Many-to-Many with self using intermediate table relationships.follower_id and followed_id, One-to-Many with photos.user_id and photo_comments.user_id)
    • email
    • password
    • created_at
    • updated_at
  • user_profiles

    • id
    • user_id (One-to-One with users.id)
    • name
    • profile_photo
  • relationships

    • id
    • follower_id (One-to-One with users.id)
    • followed_id (One-to-One with users.id)
    • created_at
    • updated_at
  • photos

    • id (One-to-Many with photo_comments.user_id)
    • user_id (One-to-One with users.id)
    • location
    • description
    • created_at
    • updated_at
  • photo_comments

    • id
    • user_id (One-to-One with users.id)
    • photo_id (One-to-One with photos.id)
    • message
    • created_at
    • updated_at

Let's go ahead and create these tables. For this project, I'll be using MySQL; feel free to copy and paste these commands.

1
CREATE DATABASE `instapics`;
2
USE `instapics`;
3
4
CREATE TABLE `instapics`.`users` (
5
  `id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
6
  `email` VARCHAR(100) NOT NULL,
7
  `password` VARCHAR(100) NOT NULL,
8
  `created_at` DATETIME NOT NULL,
9
  `updated_at` DATETIME NOT NULL,
10
  PRIMARY KEY (`id`),
11
  UNIQUE INDEX `Index_email`(`email`)
12
)
13
ENGINE = InnoDB
14
CHARACTER SET utf8 COLLATE utf8_general_ci;
15
16
CREATE TABLE `instapics`.`user_profiles` (
17
  `id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
18
  `user_id` INTEGER UNSIGNED NOT NULL,
19
  `name` TEXT NOT NULL,
20
  `profile_photo` TEXT NOT NULL,
21
  PRIMARY KEY (`id`),
22
  UNIQUE INDEX `Index_user_id`(`user_id`),
23
  CONSTRAINT `FK_user_profiles_user_id` FOREIGN KEY `FK_user_profiles_user_id` (`user_id`)
24
    REFERENCES `users` (`id`)
25
    ON DELETE CASCADE
26
    ON UPDATE CASCADE
27
)
28
ENGINE = InnoDB
29
CHARACTER SET utf8 COLLATE utf8_general_ci;
30
31
CREATE TABLE `instapics`.`relationships` (
32
  `id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
33
  `follower_id` INTEGER UNSIGNED NOT NULL,
34
  `followed_id` INTEGER UNSIGNED NOT NULL,
35
  `created_at` DATETIME NOT NULL,
36
  `updated_at` DATETIME NOT NULL,
37
  PRIMARY KEY (`id`),
38
  UNIQUE INDEX `Index_follower_id_followed_id`(`follower_id`, `followed_id`),
39
  CONSTRAINT `FK_relationships_follower_id` FOREIGN KEY `FK_relationships_follower_id` (`follower_id`)
40
    REFERENCES `users` (`id`)
41
    ON DELETE CASCADE
42
    ON UPDATE CASCADE,
43
  CONSTRAINT `FK_relationships_followed_id` FOREIGN KEY `FK_relationships_followed_id` (`followed_id`)
44
    REFERENCES `users` (`id`)
45
    ON DELETE CASCADE
46
    ON UPDATE CASCADE
47
)
48
ENGINE = InnoDB
49
CHARACTER SET utf8 COLLATE utf8_general_ci;
50
51
CREATE TABLE `instapics`.`photos` (
52
  `id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
53
  `user_id` INTEGER UNSIGNED NOT NULL,
54
  `location` TEXT NOT NULL,
55
  `description` TEXT NOT NULL,
56
  `created_at` DATETIME NOT NULL,
57
  `updated_at` DATETIME NOT NULL,
58
  PRIMARY KEY (`id`),
59
  CONSTRAINT `FK_photos_user_id` FOREIGN KEY `FK_photos_user_id` (`user_id`)
60
    REFERENCES `users` (`id`)
61
    ON DELETE CASCADE
62
    ON UPDATE CASCADE
63
)
64
ENGINE = InnoDB
65
CHARACTER SET utf8 COLLATE utf8_general_ci;
66
67
CREATE TABLE `instapics`.`photo_comments` (
68
  `id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
69
  `user_id` INTEGER UNSIGNED NOT NULL,
70
  `photo_id` INTEGER UNSIGNED NOT NULL,
71
  `message` TEXT NOT NULL,
72
  `created_at` DATETIME NOT NULL,
73
  `updated_at` DATETIME NOT NULL,
74
  PRIMARY KEY (`id`),
75
  CONSTRAINT `FK_photo_comments_user_id` FOREIGN KEY `FK_photo_comments_user_id` (`user_id`)
76
    REFERENCES `users` (`id`)
77
    ON DELETE CASCADE
78
    ON UPDATE CASCADE,
79
  CONSTRAINT `FK_photo_comments_photo_id` FOREIGN KEY `FK_photo_comments_photo_id` (`photo_id`)
80
    REFERENCES `photos` (`id`)
81
    ON DELETE CASCADE
82
    ON UPDATE CASCADE
83
)
84
ENGINE = InnoDB
85
CHARACTER SET utf8 COLLATE utf8_general_ci;

Alternatively, you could use migrations, but we'll review those in a future lesson.


Step 2 - Setup Laravel's Database Configuration

Before doing anything with Laravel models, we need to setup our Laravel installation's database configuration. Open application/config/database.php, to find some of these settings:

  • profile - setting this to true will log all SQL querie times into the Laravel logs. Leave this as true for now.
  • fetch - the type of returned data from PDO. Default value is PDO::FETCH_CLASS and should be left like so.
  • default - this is the name of the connection settings used by the application. The name refers to the index in the $connections array just below
  • connections - an associative array of the possible connections for your application.

    • driver - the database server type. This can be pgsql, sqlite, mysql or sqlsrv
    • host - the host name of your database server
    • database - the database name
    • username - username to use on the database server
    • password - password to use on the database server
    • charset - charset to use on the database server
    • prefix - table prefix on the database, if any
  • redis - if you plan on using Laravel's Redis library, you can set up the server information here.

For the purposes of this tutorial, we'll be using MySQL. Your database.php file should look something like this (I removed the comments, but they should be fine to keep):

1
return array(
2
	'profile' => true,
3
	'fetch' => PDO::FETCH_CLASS,
4
	'default' => 'mysql',
5
	'connections' => array(
6
		'mysql' => array(
7
			'driver'   => 'mysql',
8
			'host'     => 'localhost',
9
			'database' => 'instapics',
10
			'username' => 'root',
11
			'password' => '(yourpassword)',
12
			'charset'  => 'utf8',
13
			'prefix'   => '',
14
		),
15
	),
16
	'redis' => array(
17
		'default' => array(
18
			'host'     => '127.0.0.1',
19
			'port'     => 6379,
20
			'database' => 0
21
		),
22
	)
23
);

Step 3 - Creating Your First Laravel Model

Begin by creating a Laravel model inside the application/models folder. Create user.php inside, and add the following code:

1
class User extends Eloquent {
2
3
}

Now, based on our review of what the User's relationships are, we need to code the relationship methods for all of them:

1
class User extends Eloquent {
2
    
3
    //setting $timestamp to true so Eloquent

4
    //will automatically set the created_at

5
    //and updated_at values

6
    public static $timestamps = true;
7
    
8
    public function user_profile()
9
    {
10
        return $this->has_one('User_Profile');
11
    }
12
    
13
    public function followers()
14
    {
15
        return $this->has_many_and_belongs_to('User', 'relationships', 'followed_id', 'follower_id');
16
    }
17
    
18
    public function following()
19
    {
20
        return $this->has_many_and_belongs_to('User', 'relationships', 'follower_id', 'followed_id');
21
    }
22
    
23
    public function photos()
24
    {
25
        return $this->has_many('Photo');
26
    }
27
    
28
    public function photo_comment()
29
    {
30
        return $this->has_many('Photo_Comment');
31
    }
32
}

Noticeably, we make use of some advanced Many-to-Many functionality here, due to the table structure of our follower model (i.e. the users table references the relationships table which references the users table again). The has_many_and_belongs_to function has the following method signature:

1
/**

2
 * Get the query for a many-to-many relationship.

3
 *

4
 * @param  string        $model

5
 * @param  string        $table

6
 * @param  string        $foreign

7
 * @param  string        $other

8
 * @return Relationship

9
*/
10
public function has_many_and_belongs_to($model, $table = null, $foreign = null, $other = null)

This actually lets us create a model that has a Many-to-Many relationship with itself (i.e. Users follow other Users). We use followers and following method names on the User model to enable us to get a User's followers or get all the users that a single User is following, respectively.

Following the User model, create the other models. When you finish, you should have:

  • application/models/photo.php
  • application/models/photo_comment.php
  • application/models/relationship.php
  • application/models/user.php
  • application/models/user_profile.php

These files will be in the tutorial's Git repository, so if you prefer to download them, you can find them here: https://github.com/nikkobautista/laravel-tutorial


Step 4 - Create the User Functions for Instapics

Let's begin using our models by creating some of the user functions we'll be needing in the application. First up: user registration. From the previous tutorial, we've already created a Registration/Login Form on the home page. Right now, it's not doing anything, but let's hook it up to a User controller, authenticate action. Create application/controllers/user.php with the following code:

1
class User_Controller extends Base_Controller
2
{    
3
    public function action_authenticate()
4
    {
5
        
6
    }
7
}

Open application/views/home/index.blade.php and look for the login form. Update the form on Line 18 to submit to the action_authenticate() method:

1
<form class="well" method="POST" action="user/authenticate">

Going back to the User_Controller, let's place some code in action_authenticate():

1
class User_Controller extends Base_Controller
2
{    
3
    public function action_authenticate()
4
    {
5
        $email = Input::get('email');
6
        $password = Input::get('password');
7
        $new_user = Input::get('new_user', 'off');
8
        
9
        if( $new_user == 'on' ) {
10
            try {
11
                $user = new User();
12
                $user->email = $email;
13
                $user->password = Hash::make($password);
14
                $user->save();
15
                Auth::login($user);
16
            
17
                return Redirect::to('dashboard/index');
18
            }  catch( Exception $e ) {
19
                echo "Faield to create new user!";
20
            }
21
        } else {
22
            $credentials = array(
23
                'username' => $email,
24
                'password' => $password
25
            );
26
            if( Auth::attempt($credentials)) {
27
                return Redirect::to('dashboard/index');
28
            } else {
29
                echo "Failed to login!";
30
            }
31
        }
32
    }
33
}

Let's break-down what we've done here so far:

  • We use the Input library to get the inputs from the submitted form
  • If the $new_user flag was checked, we create a new User, using the Hash library to generate an encrypted password.
  • Log in the new User with the Auth library
  • If the $new_user flag was unchecked, we create a $credentials array, and use it with the Auth library.
  • If Auth::attempt is true, it means the credentials were correct and we're logged in
  • Else, it means the credentials were wrong.
  • In both scenarios, redirect to the Dashboard controller, index action when successful, and echo a failure message if not.

The Input library

The basic function of the Input library is to enable retrieval of form data. For example, in the User controller, we used Input::get('email'); to get the email value from the form. It's important to note that the get method is used for all types of requests and not just the $_GET array.

You can read more about the Input library here: http://laravel.com/docs/input#input

The Auth library

Laravel comes with its own authentication mechanism, the Auth library. It can do the following features with regards to user authentication.

Creating a hashed password

We can make use of the Hash library like so:

1
$password = Hash::make($plaintext_password);

This method creates a salted and hashed password for a user, using the encryption key we set in the configuration file. To check if a hash is correct, you can use:

1
if( Hash::check($plaintext_password, $hashed_password) == true ) {
2
    echo 'Password is correct.';
3
}

Logging in a user

For this, we use the Auth library's attempt method. Before that, though, we build a $credentials array, which is an associative array with a username and password indexes:

1
$credentials = array(
2
    'username' => 'yourname@youremail.com',
3
    'password' => 'yourpassword'
4
);
5
if( Auth::attempt($credentials) == true ) {
6
    echo 'User is logged in';
7
} else {
8
    echo 'Credentials failed';
9
}

It's important to keep in mind that once an attempt is "successful", the user is automatically logged in.

Loggin a user in/out

Sometimes, we'll need to login a user without using the attempt method (such as when logging in a user from a link inside an email, or post-registration). We can do that with the Auth::login method:

1
Auth::login($user); //where $user is a User object

2
Auth::login(42); //where 42 is the User's ID

On the other hand, we also have an Auth::logout method for logging users out:

1
Auth::logout();

This will terminate the user's session.

Retrieving the logged in user's data

The Auth::user method allows us to retrieve the logged in user object, as well as any information attached to it:

1
$email = Auth::user()->email;
2
$created_at = Auth::user()->created_at;

Configuring the Auth library

The Auth library has to be configured before using it (though the defaults will work with most projects, like this one). The configuration variables are:

  • driver - this can be either eloquent or fluent. Developers can write their own drivers by extending the Driver class in laravel/auth/drivers.
  • username - this is the column name of whatever represents your user's "username" in the database table.
  • model - when using Eloquent, this is the model class that the Auth library uses
  • table - when using the Fluent authentication driver, this determines the database table for the users in the application

Our project, Instapics, uses the default values in the auth.php configuration file, so we don't need to change anything in it.

Going back to the project, try testing out the login/registration features of Instapics! Pretty nifty, huh? You'll notice though that we don't have anything in the Dashboard controller yet, so let's work on that next.


Step 5 - Create the Instapics Dashboard

The first thing we need to do is create the Dashboard controller, with the index action. Create the file application/controllers/dashboard.php and put in the following code:

1
class Dashboard_Controller extends Base_Controller
2
{
3
    public function action_index()
4
    {
5
        $photos = Auth::user()->photos()->order_by('created_at', 'desc')->order_by('id', 'desc')->get();
6
        return View::make('dashboard.index', array('photos' => $photos));
7
    }
8
}

Now we need to create the Dashboard's index page. Create application/views/dashboard/index.blade.php and append the following:

1
@layout('layouts/main')
2
3
@section('navigation')
4
@parent
5
<li><a href="user/logout">Logout</a></li>
6
@endsection
7
8
@section('content')
9
<div class="row">
10
    <div class="span3">
11
        <div class="well sidebar-nav">
12
            <ul class="nav nav-list">
13
                <li class="nav-header">Followers</li>
14
            </ul>
15
            <div style="margin-left: 10px">
16
                @forelse (Auth::user()->followers as $follower)
17
                    <div style="float: left; width: 30px; margin: 0px 3px 3px 5px;">
18
                        <img src="http://nettuts.s3.amazonaws.com/2069_laravel_2/http://gravatar.com/avatar/{{ md5(strtolower(trim($follower->email))) }}?s=25&d=retro" alt="Follower" title="{{ $follower->email }}" />
19
                    </div>
20
                @empty
21
                    <div>You have no followers.</div>
22
                @endforelse
23
                <div style="clear: both"></div>
24
            </div>
25
            
26
            <ul class="nav nav-list">
27
                <li class="nav-header">Following</li>
28
            </ul>
29
            <div style="margin-left: 10px">
30
                @forelse (Auth::user()->following as $following)
31
                    <div style="float: left; width: 30px; margin: 0px 3px 3px 5px;">
32
                        <img src="http://nettuts.s3.amazonaws.com/2069_laravel_2/http://gravatar.com/avatar/{{ md5(strtolower(trim($following->email))) }}?s=25&d=retro" alt="Following" title="{{ $following->email }}" />
33
                    </div>
34
                @empty
35
                    <div>You are not following anybody.</div>
36
                @endforelse
37
                <div style="clear: both"></div>
38
            </div>
39
        </div>
40
    </div>
41
    <div class="span9">
42
        <h1>Your Photos</h1>
43
        @forelse ($photos as $photo)
44
        <div class="well" style="text-align: center">
45
            <img src="http://nettuts.s3.amazonaws.com/2069_laravel_2/{{ $photo->location }}" alt="{{ $photo->description }}" title="{{ $photo->description }}" />
46
            <p>{{ $photo->description }}</p>
47
        </div>
48
        @empty
49
        <div class="alert alert-info">
50
            <h4 class="alert-heading">Awww!</h4>
51
            <p>Seems like you don't have any photos yet. <a href="#">Upload a new one?</a></p>
52
        </div>
53
        @endforelse
54
    </div>
55
</div>
56
@endsection

Refresh the dashboard page, you should see this:

Instapics DashboardInstapics DashboardInstapics Dashboard

Looking a bit bare? Add this to the Dashboard controller, and run it by accessing dashboard/insert_test_data on your browser:

1
public function action_insert_test_data()
2
{
3
    $logged_in_user = Auth::user();
4
    
5
    for( $x = 0; $x < 10; $x++ ) {
6
        $email = rand().'@gmail.com';
7
        $user = new User();
8
        $user->email = $email;
9
        $user->password = Hash::make($email);
10
        $user->save();
11
        
12
        $logged_in_user->followers()->attach($user->id);
13
        if( $x > 5 ) {
14
            $logged_in_user->following()->attach($user->id);
15
        }
16
    }
17
    
18
    $photos = array(
19
        array(
20
            'user_id' => $logged_in_user->id,
21
            'location' => 'http://farm6.staticflickr.com/5044/5319042359_68fb1f91b4.jpg',
22
            'description' => 'Dusty Memories, The Girl in the Black Beret (http://www.flickr.com/photos/cloudy-day/)'
23
        ),
24
        array(
25
            'user_id' => $logged_in_user->id,
26
            'location' => 'http://farm3.staticflickr.com/2354/2180198946_a7889e3d5c.jpg',
27
            'description' => 'Rascals, Tannenberg (http://www.flickr.com/photos/tannenberg/)'
28
        ),
29
        array(
30
            'user_id' => $logged_in_user->id,
31
            'location' => 'http://farm7.staticflickr.com/6139/5922361568_85628771cd.jpg',
32
            'description' => 'Sunset, Funset, Nikko Bautista (http://www.flickr.com/photos/nikkobautista/)'
33
        )
34
    );
35
    $logged_in_user->photos()->save($photos);
36
}

When you refresh the page, you'll see what it looks like with the sample data inserted:

Instapics Dashboard w Sample DataInstapics Dashboard w Sample DataInstapics Dashboard w Sample Data

Conclusion

In the second part of our Laravel series, we learned:

  • Some background on what "Models" are
  • What the Eloquent ORM is
  • How to set up Laravel's database configuration
  • How to create your first Laravel Model
  • The basic functions of the Auth and Input libraries
  • Making use of the Eloquent ORM in a view

Eloquent really is an awesome ORM implementation - it's fast and has tons of features that make database interactions in any application as simple as possible.

Next in our Web Applications from Scratch with Laravel series, we'll learn more about using Laravel's filters, Laravel's Validation library, and how to work with files in Laravel!

What do you think about Laravel's Eloquent ORM? Is it something that you find useful? Let me know in the comments! And, if you're a Tuts+ Premium member, stay tuned for our upcoming Laravel Essentials course!

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.