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

iscromanpc's avatar

Get data from 3 tables with relationship

Hello. I'm new in this site and also new in Laravel. Could you please help me with this question?

I have this DB table structure:

  • user

    • id
    • name
    • city_id
  • city

    • id
    • name
    • region_id
  • region

    • id
    • name

I can get the City name as there is a direct relationship with User table. I do this in the User model with this lines:

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

Now I'm triying to get the region name related with a user but I can´t.

I tried like this but I think that does not work on that way.

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

Also in this way is not working:

DB::table('city') ->join('region', 'region.ide', '=', 'city.region_id') ->get();

A already reviewed the information with Eloquent Relationships but maybe i'm not understanding the concept.

Thanks in advance for your help.

0 likes
7 replies
iscromanpc's avatar

Thank you for your prompt answer @jlrdw.

I know that this is a common question when someone is starting with Laravel and I already searched in this site and other sources of information, but I'm not unable yet to resolve my question.

Mostly i'm getting this error:

Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation

The code that i'm implementing on my User model is something like this:

public function region()
{
    $region = Region::find(1)->cities()->get();

    return $region;
}

Thank you for your help.

kirankrishnan's avatar

@iscromanpc

City Model

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

Now you can access the region name for the user as:

$user = User::find(1)->city()->region->name

[code not tested]

2 likes
iscromanpc's avatar
public function region()
{
    $user = User::find(1)->city()->region->name;

    return $user;
}

I'm getting and error with your solution implemented on my model:

Undefined property: Illuminate\Database\Eloquent\Relations\BelongsTo::$region

What you suggest, should be implemented on the User model?

How I should return the value $user to be available on my User view?

Thank you @kirankrishnan

iscromanpc's avatar
iscromanpc
OP
Best Answer
Level 1

I was abble to fix it with the help of one friend.

Finally I'm getting the data from one user, in my controller using something like:

$user = \DB::table('user') ->select( 'user.id', 'user.name', 'city.name as city_name', 'region.name as region_name') ->leftJoin('city', 'user.city_id', '=', 'city.id') ->leftJoin('region', 'city.region_id', '=', 'region.id') ->where('operator.id', '=', $id)->first();

Thanks for try to help me.

kuamatzin's avatar

The @kirankrishnan is correct, the way you are doing the things with the left join is complicated, In you City Model add this lines:

class City extends Model
{
    public function region() {
            return $this->belongsTo('App\Region');
    }
}

class User extends Model
{
    public function city() {
            return $this->belongsTo('App\City');
    }
}

$region_name = User::find(1)->city->region->name

1 like

Please or to participate in this conversation.