Scenario - I have 3 tables > Country, State, City. City have stateid column, State have countryid column, country have no reference.
City Model methods
public function state(){
return $this->belongsTo('App\State', 'stateid');
}
State Model methods
public function country(){
return $this->belongsTo('App\Country', 'countryid');
}
public function cities(){
return $this->hasMany('App\City', 'stateid');
}
Country Model methods
public function states()
{
return $this->hasMany('App\State', 'countryid');
}
Problem - I want to get the list of cities in a country. How can I create a method in Country Model like this? -
public function cities(){
return $this->states()->cities(); //Calling hasMany on builder is not possible.
}
Similarly, a method to country name from city model.