虽然急于加载关系,但它返回空数组作为结果,但在获得结果后关系工作正常。
包含函数的内部关系$this在预先加载时没有属性值。
我试图在没有急切加载的情况下急切加载关系,$this
具有所有属性值。
- 控制器
$temp = StipendCalcVols::with('stipendCalcTypes')->where('vol_id', 110)->first();
- 型
public function stipendCalcTypes() {
dd($this); // null attributes
return $this->hasMany(StipendCalcTypes::class, 'table_id', 'vol_id')
->where('stipend_calc_id', $this->stipend_calc_id)->where('table_name', 'volunteers');
}
$this
应具有属性值。
试试这个
$temp = StipendCalcVols::where('vol_id', 110)->first();
$temp = $temp->load('stipendCalcTypes');
试试这个:
而不是:
$this->stipend_calc_id
使用这个:
$this->attributes['stipend_calc_id']