Multiple MorphToMany relationships #197
Answered
by
staudenmeir
flashmediasolutions
asked this question in
Q&A
-
I am having a difficult time putting together the right format with this relationship. I want to get all the
Any help, or any advice on a better way would be greatly appreciated! Thanks |
Beta Was this translation helpful? Give feedback.
Answered by
staudenmeir
Jun 16, 2023
Replies: 1 comment 1 reply
-
Hi @flashmediasolutions,
class Store extends Model
{
use \Staudenmeir\EloquentHasManyDeep\HasRelationships;
use \Staudenmeir\LaravelMergedRelations\Eloquent\HasMergedRelationships;
public function examples()
{
return $this->mergedRelationWithModel(Example::class, 'all_examples');
}
public function examplesA()
{
return $this->hasManyDeepFromRelations($this->sourcesA(), (new SourceA())->examples());
}
public function examplesB()
{
return $this->hasManyDeepFromRelations($this->sourcesB(), (new SourceB())->examples());
}
public function examplesC()
{
return $this->hasManyDeepFromRelations($this->sourcesC(), (new SourceC())->examples());
}
public function sourcesA()
{
return $this->morphedByMany(SourceA::class, 'storeable');
}
public function sourcesB()
{
return $this->morphedByMany(SourceB::class, 'storeable');
}
public function sourcesC()
{
return $this->morphedByMany(SourceC::class, 'storeable');
}
}
use Staudenmeir\LaravelMergedRelations\Facades\Schema;
Schema:: createOrReplaceMergeView(
'all_examples',
[(new Store())->examplesA(), (new Store())->examplesB(), (new Store())->examplesC()]
);
$allExamples = Store::find($id)->examples; If that works for you, we'll take a look at the reverse relationship. |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
flashmediasolutions
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @flashmediasolutions,
Combine this package with one of my other ones:
https://github.com/staudenmeir/laravel-merged-relations