Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ditchmonkey's avatar

Basic overview of querying relationships with eloquent

With help from the community here I have finally been able to query the results of a table with eloquent. As a next step, I was hoping to get some help querying a simple relationship. I've tried some tutorials but I'm getting errors and there are parts I'm just not understanding. I have two tables:

towns
+---------+------------------------+-----------------+
| id (PK) | name                   | state_id (FK)   |
+---------+------------------------+-----------------+
| 1       | Los Angeles            | 2               |
| 2       | Seattle                | 3               |
| 3       | Portland               | 1               |
| 3       | San Francisco          | 2               |
+----------------------------------+-----------------+

states
+---------+------------------------+
| id (PK) | name                   |
+---------+------------------------+
| 1       | Oregon                 |
| 2       | California             |
| 3       | Washington             |
+----------------------------------+

Can someone show me what the models would look like and how to display all the towns that belong to California?

0 likes
9 replies
dehumanizer's avatar
//Models
//app/Town.php
<?php namespace App;

use Illuminate\Database\Eloquent\Model as Eloquent;

class Town extends Eloquent {

    public function state()
    {
        return $this->belongsTo('App\State');
    }
}

//app/State.php
<?php namespace App;

use Illuminate\Database\Eloquent\Model as Eloquent;

class State extends Eloquent {

    public function towns()
    {
        return $this->hasMany('App\Town');
    }
}

//Controller
$california = State::find(2);

//View
@foreach($california->towns as $town)
    {{ $town->name }}
@endforeach
pmall's avatar

That said, instead of hasMany('Post') you would use hasMany('App\Models\Post') or whatever the namespace you have.

If using php 5.5 I like to use hasMany(Post::class), this way if the models namespace change during development no need to update every relationships :)

JarekTkaczyk's avatar

@pmall Yep, that's true, it's better way for 5.5 as long as all the models are in one namespace.

However I wouldn't worry about that at all ;) If you for any reason change the namespace for your models, you can run simple refactoring in matter of seconds.

Ditchmonkey's avatar

Does that mean that all Laravel 4 models will need to be updated with namespaces when upgrading to Laravel 5?

dehumanizer's avatar

Yes! But don't panic, it is kinda cool. I upgraded last week. I haven't done a single composer dump-autoload yet.

Ditchmonkey's avatar

@dehumanizer I'm trying your code but I'm getting the state name returned, not the names of the towns in that state. For debugging I added

return $california;

to my controller which returns

{"id":5,"state_name":"California"}

I'm struggling to understand the details of the models. How does eloquent know which column is the foreign key?

pmall's avatar

I'm struggling to understand the details of the models. How does eloquent know which column is the foreign key?

It is based ont the models names. It is a convention. Like tables names which are models names pluralized.

Really everyting is explained on laravel.com doc or in jeffrey's screencasts. Take the time to read/watch all this stuff.

Ditchmonkey's avatar

@pmall Did that. Like I said in my previous thread, I'm having a difficult time translating the lessons into something that works for my particular project where eloquent is concerned. Just need to work through some examples and get them working.

Please or to participate in this conversation.