1
情景 - 我有3個表>國家,州,市。市有州立專欄,州有國家專欄,國家沒有參考。Laravel 5 - 雄辯模型 - 系列3表
城市模型方法
public function state(){
return $this->belongsTo('App\State', 'stateid');
}
國家示範性方法
public function country(){
return $this->belongsTo('App\Country', 'countryid');
}
public function cities(){
return $this->hasMany('App\City', 'stateid');
}
國家示範方法
public function states()
{
return $this->hasMany('App\State', 'countryid');
}
問題 - 我想在一個國家的城市名單。我如何在這樣的國家模式中創建一個方法? -
public function cities(){
return $this->states()->cities(); //Calling hasMany on builder is not possible.
}
同樣,從城市模型的國家名稱的方法。
你要找因爲是'hasManyThrough()'。請參閱:https://laravel.com/docs/5.1/eloquent-relationships#has-many-through – Mysteryos
@Mysteryos - 謝謝,工作。 – asachanfbd
@Mysteryos - 有沒有辦法從城市中獲取國家? – asachanfbd