I am using Eloquent to pull users out of my database, and i want to "join" the postal code from my users table with the postal code from my cities table, to retrieve the city name.
Heres a simplified version of my tables:
users =>
id
username
password
postal_code
cities =>
postal_code
city_name
In my User model i have a method called city() that declares the relationship with my cities table:
public function city() {
return $this->hasOne('cities', 'postal_code');
}
The problem now is when i try to do User::with('city')->find($user_id), the value being passed to the method is the primary key, the id, instead of the postal_code.
So my query ends up being (this is for the user with id 1):
select * from `cities` where `cities`.`postal_code` in ('1')
Is there someway to specify that i want to pass the postal_code instead?