I'm in trouble with eav model. Basically; i have three tables. User, UserInfo, Country.
User userinfo Country
--------- ------------ ------------
id id id
username user_id country_name
pass option_id
etc option_value
I connected user and userinfo models succesfully. There is a no problem. I retrieving user infos in user model with this:
$this->info->where('option_id', $key)->first()->option_value;
My problem is right there. I'am storing "country_id" in option_id and option_value.
In example:
userinfo
--------
id 1
user_id 5
option_id country_id
option_value 3
I am running this query from user model
$this->info->where('option_id', 'country_id')->first()->option_value;
It returns 3. There is a no problem. My problem is i couldn't get country_name. I don't know which relationship to use for this. I can make this using "DB" class but then i need use another queries for all of theese types. It's not the best solution.
Can someone help me?
Edit : I am looking for like this solution (if it's possible):
$this->info->where('option_id', $key)->first()->option_value->country_name;
Edit : I am using laravel 5.1
The Country model belongs to the userinfo. On your userinfo model, you should define the relationship.
UserInfo Class
public function country()
{
return $this->belongsTo('App\Country', 'local_key', 'parent_key');
}
In this case, your 'local_key'
would be 'option_id'
and your 'parent_key'
would be 'id'
.
Once you've defined the relationship, you can get all attribute of country like this:
$this->info->where('option_id', $key)->first()->country()->get();