Laravel 5.3雄辩3表联合使用模型 [英] Laravel 5.3 eloquent 3 table join using models
问题描述
我阅读了这个帖子,这有助于帮助,但需要更多的指导。 p>
我需要一组特定的 $ batteryid
(结果)和一个特定的 $年龄
(在测试模型)和特定 $ gender
(运动员)。每个测试属于一个运动员。每个测试都有很多结果。
模型电池:
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');
}
}
我得到以下结果,但它是两个查询:
$ 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 。$'''''''''''''''''''''''''' ('test-> athlete()。gender','='$ gender)
- > where('battery_id','=',$ battery)
- > get();你可以使用 whereHas / code>的条件和与
方法渴望加载它。 $ results = Result :: whereHas('test',function($ q)use($ age){
$ q-> where('age','=',$ age);
$ q-> whereHas('athlete',function($ q){
$ q->其中('gender','male');
});
})
- > with('test')
- > whereHas 电池功能($ q)使用($ battery){
$ q-> where('battery_id','=',$ battery);
})
- > ;('battery')
- > get();
I read this post which helped, but need more guidance please.
I need a set of Results for a particular $batteryid
(on Result) and a particular $age
(on Test model) and a particular $gender
(from Athlete). Each Test belongs to one athlete. Each test has many results.
Model Battery:
class Battery extends Model{
public function results(){
return $this->hasMany('App\Result');
}
}
Model 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');
}
}
Model Test:
class Test extends Model{
public function athlete(){
return $this->belongsTo('App\Athlete', 'athlete_id');
}
public function results(){
return $this->hasMany('App\Result');
}
}
I get the correct results with the following, but it's two queries:
$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();
I'd like to use model approach, but am completely stuck. test.age
not recognised and not sure how to get the age from athlete model. My attempt:
$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();
解决方案 You can use whereHas
for the condition and with
method for eager loading it.
$results = Result::whereHas('test', function($q) use ($age) {
$q->where('age', '=', $age);
$q->whereHas('athlete', function ($q) {
$q->where('gender', 'male');
});
})
->with('test')
->whereHas('battery', function($q) use($battery) {
$q->where('battery_id', '=', $battery);
})
->with('battery')
->get();
这篇关于Laravel 5.3雄辩3表联合使用模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!