【发布时间】:2019-06-27 03:07:05
【问题描述】:
是否可以通过兄弟模型的belongsTo 关系检索父模型的hasMany 关系。我有以下Models:
汽车
public function wheels() {
return $this->hasMany('App\Models\Wheel');
}
public function seats() {
return $this->hasMany('App\Models\Seat');
}
轮子
// @property int|null $car_id Type: int(10) unsigned, Extra: , Default: null, Key: MUL
public function car() {
return $this->belongsTo('App\Models\Car');
}
座位
// @property int|null $car_id Type: int(10) unsigned, Extra: , Default: null, Key: MUL
public function car() {
return $this->belongsTo('App\Models\Car');
}
我想做的是取回给定座位的汽车车轮 ($seat->wheels):
座位
public function car() {
return $this->belongsTo('App\Models\Car');
}
public function wheels() {
// Works
// return $this->car->wheels;
// What I would like to do, but doesn't work
return $this->hasManyThrough('App\Models\Wheel', 'App\Models\Car');
}
【问题讨论】:
标签: laravel eloquent has-many-through