次の非常に単純な例を考えます。
カントリークラス
class Country extends Eloquent {
protected $table = "countries";
protected $fillable = array(
'id',
'name'
);
public function state() {
return $this->hasMany('State', 'country_id');
}
}
州クラス
class State extends Eloquent {
protected $table = "states";
protected $fillable = array(
'id',
'name',
'country_id' #foreign
);
public function country() {
return $this->belongsTo('Country', 'country_id');
}
}
id
または国のに基づいて、すべての州を一覧表示するにはどうすればよいですかname
。
例:
State::with('country')->where('country.id', '=', 1)->get()
上記はcountry
、クエリの一部ではないため、領域を返します (Eloquent は後で where 句の後に結合をアタッチする必要があります)。