我正在使用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)
。但是,有没有更好的方法来返回这两个模型并在新函数中访问它们?
我请你举个例子。
提前谢谢!答案 0 :(得分:2)
你可以合并它们
$mergedResult = $instruments->merge($team);
如果两个模型之间存在关系,则可以获得相同模型的数据
$teamWithIncreaments = Team::with('increaments')->all();