Pedro
Pedro

Reputation: 1477

Relation between users and country table

Im trying to get the user country but isnt working, my example code is:

User Model:

public function countryName()
    {
        return $this->hasOne('App\Country');
    }


Tables:
User:
- id;
- name;
- country_id;

Countries:
-id ;
- name;

Upvotes: 1

Views: 458

Answers (1)

Alexey Mezenin
Alexey Mezenin

Reputation: 163978

Set relation as:

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

To get the country do this:

$user->country->name;

Upvotes: 2

Related Questions