如何在Laravel的单个集合中结合三个多对多的关系?

问题描述 投票:0回答:1

我有以下几种关系:

演员< - > theater_play,演员< - >音乐剧,演员< - >芭蕾舞剧

如何在Laravel中创建单个数组来显示这些关系的所有结果?

我现在有了:

$actor->theaterPlays$actor->musicals$actor->balletPlays

我需要这样的东西:

$actor->allPerformances

编辑:

我必须通过名字或表演日期等来订购它们。

laravel laravel-5 many-to-many
1个回答
0
投票

未经测试,但您应该能够在accessor模型中创建一个新的Actor,它负责将所有类型合并在一起:

public function getAllPerformancesAttribute()
{
    return $this->theaterPlays()
        ->get()
        ->merge($this->musicals()->get())
        ->merge($this->balletPlays()->get())
        ->all();
}
© www.soinside.com 2019 - 2024. All rights reserved.