Laravel 4渴望在多对多关系中加载


Laravel 4 Eager Loading in Many to Many Relationship

尝试使用多对多关系对Laravel 4进行简单的热切加载。我的模特看起来像。

class Facility extends Eloquent {
    public function photos(){
            return $this->belongsToMany('Photo');
    }
}
class Photo extends Eloquent {
    public function facilities(){
            return $this->belongsToMany('Facility');
    }
}

Tabes是根据Laravel标准设置的。当我尝试使用加载时

$facilities = Facility::with('Photo')->get();

我最终出现了一个Laravel错误

Call to undefined method Illuminate'Database'Query'Builder::photo()

知道这里做错了什么吗?

您应该尝试:

$facilities = Facility::with('photos')->get();

请记住,传递给with()的参数是方法,而不是模型,所以如果模型中有另一个方法,比如:location(),您将调用:

$facilities = Facility::with(['photos', 'location'])->get();