[英]How to use the `where` method along with the `with` method in laravel 4?
Given the following, very simple, example: 给出以下非常简单的示例:
Country Class 国家级
class Country extends Eloquent {
protected $table = "countries";
protected $fillable = array(
'id',
'name'
);
public function state() {
return $this->hasMany('State', 'country_id');
}
}
State Class 州级
class State extends Eloquent {
protected $table = "states";
protected $fillable = array(
'id',
'name',
'country_id' #foreign
);
public function country() {
return $this->belongsTo('Country', 'country_id');
}
}
How can I list all the states, based on the id
or the name
of the country. 如何根据国家/地区的id
或name
列出所有州。
Example: 例:
State::with('country')->where('country.id', '=', 1)->get()
The above returns an area, as country
is not part of the query (Eloquent must attach the join later, after the where clause). 上面的代码返回一个区域,因为country
不是查询的一部分(Eloquent必须稍后在where子句之后附加联接)。
I think you're either misunderstanding the relations or over-complicating this. 我认为您要么误解了关系,要么使这一问题变得过于复杂。
class Country extends Eloquent {
public function states() {
return $this->hasMany('State', 'state_id');
}
}
$country = Country::find(1);
$states = $country->states()->get();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.