1

Trying to do a simple eager load with Laravel 4 using a many to many relationships. 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. When I try to load using

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

I end up with a Laravel error

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

Any idea whats being done wrong here?

4

1 回答 1

6

你应该试试:

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

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

$facilities = Facility::with(['photos', 'location'])->get();
于 2013-07-18T21:10:18.513 回答