繁体   English   中英

渴望加载laravel 3深层关系

[英]Eager loading laravel 3 deep relationship

我得到了与州(州)相关的模型城市。

当调用一个具有city的模型时,得到了city,但是state返回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');
    }
}

使用封闭方法应该可以...

// 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#eager

暂无
暂无

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

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