Hi, I have an object which could have one or many distant relations, depending on the intermediate relation.
A Social Account can belong to a Season, or it can belong to a Contestant
A Contestant can appear on many Seasons
Each Season belongs to one Show, and each Show has many Seasons.
I want to reliably get a list of a Social Account's seasons and shows. Which would either be via the one Season it belongs to, or the many Seasons its Contestant belongs to.
I have sort of hacked it together below, and it largely works, but anything eager loaded returns null, and it errors if there isn't a relation.
Is there a way to use hasManyDeepFromRelations with a MorphTo relationship?
// Social.php
public function owner()
{
// owner_type = App\Season or App\Contestant
return $this->morphTo();
}
public function seasons()
{
// This doesn't work for eager loading
return optional($this->owner)->seasons();
}
public function shows()
{
// This works if there IS an owner (not when it's null) but doesn't work for eager loading
return $this->hasManyDeepFromRelations($this->owner(), (new ($this->owner_type))->shows());
}
// Contestant.php
public function seasons()
{
return $this->belongsToMany('App\Season')->withPivot(['role'])->withTimestamps();
}
public function shows()
{
return $this->hasManyDeepFromRelations($this->seasons(), (new \App\Season())->show());
}
// Season.php
public function seasons()
{
//return array of self
return $this->hasMany('App\Season', 'id', 'id');
}
public function shows()
{
//return the single show but wrapped in a collection
return $this->hasMany('App\Show', 'id', 'show_id');
}
public function show()
{
return $this->belongsTo('App\Show');
}
Hi, I have an object which could have one or many distant relations, depending on the intermediate relation.
I want to reliably get a list of a Social Account's seasons and shows. Which would either be via the one Season it belongs to, or the many Seasons its Contestant belongs to.
I have sort of hacked it together below, and it largely works, but anything eager loaded returns null, and it errors if there isn't a relation.
Is there a way to use hasManyDeepFromRelations with a MorphTo relationship?