从功能中返回两个模型

时间:2018-01-14 09:19:34

标签: php laravel laravel-5

我正在使用Laravel 5.5,我有一个功能,想要返回两个模型,如下所示:

public function getDetails($id)
{
    $instruments = Instruments::join('revisions', 'revisions.id', '=', 'instruments.revisions_id')
        ->orderBy('instruments.name')
        ->first();

    $team = Team::join('instruments', 'teams.instruments_id', '=', 'instruments.id')
        ->orderBy('instruments.name')
        ->get();

    $result = array($instruments, $team);
    return $result;
}

目前,我正在尝试将数据打包成以下数据$result = array($instruments, $team)。但是,有没有更好的方法来返回这两个模型并在新函数中访问它们?

我请你举个例子。

提前谢谢!

1 个答案:

答案 0 :(得分:2)

你可以合并它们

 $mergedResult = $instruments->merge($team);

如果两个模型之间存在关系,则可以获得相同模型的数据

    $teamWithIncreaments = Team::with('increaments')->all();