Laravel关系$this在使用函数访问时返回null 'with'



虽然急于加载关系,但它返回空数组作为结果,但在获得结果后关系工作正常。

包含函数的内部关系$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']

最新更新