0

我想做这样的事情。

Location::where('city', '=', 'Chicago')->chef();

有了这些关系:

class Location extends Eloquent {
    protected $table = 'locations';

    public function chef() {
        return $this->belongsTo('Chef');
    }
}

class Chef extends Eloquent {
    protected $table = 'chefs';

    public function location() {
        return $this->hasMany('Location');
    }
}
4

1 回答 1

2

这应该有效:

class Location extends Eloquent {
    protected $table = 'locations';

    public function chefs() {
        return $this->belongsTo('Chef');
    }

    public function getAllChefsByCity($city)
    {
         $this->with('chefs')->where('city', $city)->get();
    }
}

然后在您的代码中:

$array = $location->getAllChefsByCity('Chicago');
于 2013-07-07T23:38:58.923 回答