Simple relationship example in Laravel 4

Today I tried to create a simple relationship with Laravel 4 Eloquent but because I am still learning Laravel I spent more than an hour to figure it out. I searched the net ,forums etc but I simply didn’t find what I was looking for or it was for more complicated example. Relationships can be quite complicated ( like we all don’t know this 😉 ) so I am not going to go into details about what they are in this post – I am going to show you a specific solution for a specific problem.

Display the name of the country of specific person with Eloquent relationships

OK, so we have a table Customers and table Countries. Table Customers has fields like first_name,last_name, telephone etc but also field/foreign key country (int). Table Countries has the list of all countries in the world. The name of the specific country in table Countries is stored in the field short_name. We have admin and in the admin we have a page that lists all customers.

Defining the relationship between two tables

Customer can only have one country but country “has” many customers.(for example several customers can be from US). So in our case we have one-to-many relationship.

In the controller (that deals with customers) we have this code:

	public function getIndex()
	{
		// Grab all the customers
		$customers = Customer::orderBy('created_at', 'DESC')->paginate(10);
		// Show the page
		return View::make('admin/customers/index', compact('customers'));
	}

and in our admin/customers/index.blade.php view we loop through results set like this(excerpt):

      @foreach ($customers as $customer)
        <tr>
            <td>{{ $customer->first_name." ".$customer->last_name }}</td>
            <td>{{ $customer->country }}</td>
            <td>{{ $customer->telephone }}</td>
        </tr>
        @endforeach

Simple but it gives us this:

Relationships before, Laravel

Now we could create a function that would return a name of the country like this Customer::countryName($customer->country) but why do this when it can be done much more elegantly.

Laravel Eloquent relationships to the rescue

Open the Customer.php model and add this method/relationship:

    //relationship with country table 
   public function country() {
        return $this->belongsTo('Country');
    }

Now let’s go back to our admin/customers/index.blade.php view and check this line:

<td>{{ $customer->country }}</td>

We can now access the country name with this code : $customer->country()->first()->short_name

but it gets even better: Due to Eloquent’s Dynamic Properties we can shorten our call so replace the above line with:

 <td>{{ $customer->country->short_name }}</td>

Save, refresh your page and voila, we see this :

Relationship final laravel

As you can see, relationships in Laravel 4 can be very useful and elegant way to solve such problems. In this post I didn’t explain every funtion call because I didn’t want it to get too confusing. I will write more about relationships in some of the future posts.

Added: You can read more about database relationships here: Database table relationships: One-to-One vs. One-to-Many vs. Many-to-Many