1
votes

While eager loading the relationship it return empty array as result but after getting the result relationship works fine..

Inside relation containing function $this has no attribute value while eager loading.

I have tried to eager load the relationship without eager loading and $this has all the attributes value.

  • Controller
$temp = StipendCalcVols::with('stipendCalcTypes')->where('vol_id', 110)->first();
  • Model
 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 should have attributes value.

2
What attribute values should $this have at this point in your opinion?Namoshek
$this->stipend_calc_id or $this->id expecting model any attributes @NamoshekBasanta Tajpuriya
Well, when the query builder eager loads the relationship, none of these attributes are set yet. This is also because you eager load for many models at the same time, not for each one individually.Namoshek
Laravel doesn't support composite keys. Use this package: github.com/topclaudy/composhipsJonas Staudenmeir

2 Answers

0
votes

try this

$temp = StipendCalcVols::where('vol_id', 110)->first();
$temp = $temp->load('stipendCalcTypes');
0
votes

Try this:

Instead of:

$this->stipend_calc_id

Use this:

$this->attributes['stipend_calc_id']