我目前正在研究 laravel 框架,但我遇到了一些关系和急切的加载问题。
我有三个模型 A、B 和 C
我有两个关系
默认情况下(使用模型中的 $with 属性):
所以大部分时间我都使用 A 不带 B 和 B 带 C
这是我设置关系方法和急切加载的方式
class A extends Model {
...
protected $with = [];
public function bs() {
return $this->hasMany('App\Models\B');
}
}
class B extends Model {
...
protected $with = ['cs'];
public function cs() {
return $this->hasMany('App\Models\C');
}
public function a() {
return $this->belongsTo('App\Models\A');
}
}
class C extends Model {
...
public function …Run Code Online (Sandbox Code Playgroud)