我得到了一个与州和国家相关的模型城市。
当调用与城市a相关的模型时,得到城市但状态返回null。
我查询Agent::with(array('city','city.state'))->find($id);
my model city
class City extends Eloquent
{
public static $table = 'city';
public function State()
{
return $this->belongs_to('State','state_id');
}
}
使用封闭方法应该可以…
// 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');
}
}
向下滚动到下面链接的"约束急切负载",查看正在使用的封装方法https://tower.la.utexas.edu/index.php/docs/database/eloquent急切的