我得到了一个与国家有关的模范城市以及国家。
当调用与城市一起实现的模型时,获得了城市,但状态返回null。
我的查询
Agent::with(array('city','city.state'))->find($id);
我的模范城市
class City extends Eloquent
{
public static $table = 'city';
public function State()
{
return $this->belongs_to('State','state_id');
}
}
答案 0 :(得分:1)
使用机箱方法应该有效......
// Like this...
Agent::with(array('city' => function($query){
$query->with('state');
}))->find($id);
// Also, I would keep my model relationship methods lowercase
class City extends Eloquent
{
public static $table = 'city';
//public function State()
public function state() // like this
{
return $this->belongs_to('State','state_id');
}
}
在下面的链接中向下滚动到“Constraining Eager Loads”以查看正在使用的机箱方法 https://tower.la.utexas.edu/index.php/docs/database/eloquent#eager