1
私は助けてthisポストを読むが、もっとガイダンスをお願いします。Laravel 5.3 eloquent 3テーブル参加モデル
特定の$batteryid
(結果)および特定の$age
(テストモデル)と特定の$gender
(Athlete製)の結果のセットが必要です。各テストは1人のアスリートに属します。各テストには多くの結果があります。
モデルバッテリー:
class Battery extends Model{
public function results(){
return $this->hasMany('App\Result');
}
}
モデルの結果:
class Result extends Model{
public function test(){
return $this->belongsTo('App\Test', 'test_id');
}
public function battery(){
return $this->belongsTo('App\Battery', 'battery_id');
}
}
モデルのテスト:
class Test extends Model{
public function athlete(){
return $this->belongsTo('App\Athlete', 'athlete_id');
}
public function results(){
return $this->hasMany('App\Result');
}
}
私は次のようで正しい結果を得るが、それは2つのクエリです:
$tests = Test::whereHas('Athlete', function($query) use($gender){
$query->wheregender($gender);
})->where('age', $age)->get();
$test_ids = $tests->pluck('id');
$results = Result::where('battery_id', '=', $battery)
->whereIn('test_id', $test_ids)
->get();
私はモデルアプローチを使用したいと思いますが、完全に固まっています。 test.age
が認識されず、アスリートモデルから年齢を取得する方法がわかりません。私の試み:
$results = Result::with('test')
->where('test.age', '=', $age)
->join('batteries', 'batteries.id', '=', 'test.battery_id')
//->where('test->athlete().gender', '=' $gender)
->where('battery_id', '=', $battery)
->get();
ジェンダービットを追加する前に機能しました。私は今、別の構文をテストしています、おそらく2番目の$ q->出てくる必要がありますか? – louisav
あなたの選手テーブルに性別の列がありますか? – geckob
'gender'変数を使用する場合、それを関数クロージャに含める必要があります。 – geckob