gpt4 book ai didi

php - Laravel 如何将两个查询结果合并到一个对象中

转载 作者:行者123 更新时间:2023-12-02 03:07:45 24 4
gpt4 key购买 nike

我目前陷入了如何将两个查询结果合并到一个对象中的困境。下面是我的代码。

已编辑

模型方法

public static function getTeamStats($competitionId, $teamId) {
return TeamCompetitionStatistics::where('competitionId', $competitionId)
->where('teamid', $teamId)
->where('periodNumber', 0)
->get();
}

public static function getTeamPosition($competitionId, $teamId){
return self::where('latest', 1)
->where('competitionId',$competitionId)
->where('competitorId', $teamId)
->get(['position', 'streak'])
->map(function($item, $key){
$item->position = $item->position . date("S", mktime(0, 0, 0, 0, $item->position, 0));
if(strpos($item->streak, '-') !== FALSE) {
$item->streak = str_replace('-', 'L', $item->streak);
}
else {
$item->streak = 'W'.$item->streak;
}
return $item;
});
}

获取 Controller 中的值

$teamStanding = Ladder::getTeamPosition($request->competitionId, $request->id);
$teamStatistics = TeamCompetitionStatistics::getTeamStats($request->competitionId, $request->id);

$result = $teamStatistics->merge($teamStanding);

返回结果:[{'teamstand': 'data'}, {'teamstatictics': 'data'}]

预期输出:[{'teamstand': 'data', 'teamstatictics': 'data'}]

最佳答案

您可以使用 all() 函数。

$teamStanding = Ladder::getTeamPosition($request->competitionId, $request->id)->get();

$teamStatistics = TeamCompetitionStatistics::getTeamStats($request->competitionId, $request->id)->get();

$merged = $teamStatistics->merge($teamStanding);

$result = $merged->all();

// return [{'teamstanding': 'data', 'teamstatictics': 'data'}]

关于php - Laravel 如何将两个查询结果合并到一个对象中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44535539/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com