简体   繁体   English

渴望加载laravel 3深层关系

[英]Eager loading laravel 3 deep relationship

I got a model City related with State and so with Country. 我得到了与州(州)相关的模型城市。

When call a model realted with city a got the city but state return null. 当调用一个具有city的模型时,得到了city,但是state返回null。

my query 我的查询

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');
    }
}

Using an enclosure method should work... 使用封闭方法应该可以...

// 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');
    }
}

Scroll down to "Constraining Eager Loads" at the below link to see enclosure methods in use https://tower.la.utexas.edu/index.php/docs/database/eloquent#eager 在下面的链接处向下滚动到“限制急切的负载”以查看正在使用的机柜方法https://tower.la.utexas.edu/index.php/docs/database/eloquent#eager

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM