简体   繁体   English

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

[英]Laravel 4 Eager Loading in Many to Many Relationship

Trying to do a simple eager load with Laravel 4 using a many to many relationships. 尝试使用多对多关系对Laravel 4进行简单而积极的加载。 My Models look like. 我的模型看起来像。

class Facility extends Eloquent {

    public function photos(){
            return $this->belongsToMany('Photo');
    }
}

class Photo extends Eloquent {

    public function facilities(){
            return $this->belongsToMany('Facility');
    }
}

Tabes are set up according to Laravel standards. 根据Laravel标准设置标签。 When I try to load using 当我尝试加载使用

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

I end up with a Laravel error 我最终遇到了Laravel错误

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

Any idea whats being done wrong here? 知道这里做错了什么吗?

You should try: 你应该试试:

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

Remember, the argument you are passing to with() is the method, not the model, so if you have another method in the model, say: location() , you'll call: 请记住,传递给with()的参数是方法,而不是模型,因此,如果模型中还有另一个方法,例如: location() ,您将调用:

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

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

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