如何在laravel(php)中避免嵌套查询?

时间:2018-04-17 13:07:07

标签: php mysql sql laravel lumen

这里有两张桌子。我想使用优化代码从这些表中检索数据。

表格

game_sessions
Id, SessionName, StartTime

games
Id, GamesSessionId, GameName

代码A:

$sessions = DB :: select('select Id as sessionId, SessionName from game_sessions');
foreach($sessions as $session)
{
    $games = DB :: select('select Id as GameId, GameName from games where games.GameSessionId = '.$session->sessionId);  
    $session->Games = array();
    $session->Games = $games;
}
return array('status'=>true, 'session'=>$sessions);

输出:($会话)

{
    "status": true,
    "session": 
    [
        {
            "sessionId": 1,
            "SessionName": "Regular bingo Manual",
            "Games": [
                {
                    "GameId": 1,
                    "GameName": "Game1"
                }
            ]
        },
        {
            "sessionId": 2,
            "SessionName": "Regular Automatic",
            "Games": [
                {
                    "GameId": 2,
                    "GameName": "Game2"
                },
                {
                    "GameId": 3,
                    "GameName": "Game1"
                }
            ]
        },
        {
            "sessionId": 3,
            "SessionName": "RegularDoubleAction",
            "Games": [
                {
                    "GameId": 4,
                    "GameName": "Game1"
                }
            ]
        }
    ]
}

代码B:

$sessions = DB :: select('select game_sessions.Id as sessionId, SessionName, games.Id as GameId, games.GameName from game_sessions
            join games on games.GameSessionId = game_sessions.Id');
return array('status'=>true, 'session'=>$sessions);

输出:($会话)

{
    "status": true,
    "session": 
    [
        {
            "sessionId": 1,
            "SessionName": "Regular bingo Manual",
            "GameId": 1,
            "GameName": "Game1"
        },
        {
            "sessionId": 2,
            "SessionName": "Regular Automatic",
            "GameId": 2,
            "GameName": "Game2"
        },
        {
            "sessionId": 2,
            "SessionName": "Regular Automatic",
            "GameId": 3,
            "GameName": "Game1"
        },
        {
            "sessionId": 3,
            "SessionName": "RegularDoubleAction",
            "GameId": 4,
            "GameName": "Game1"
        }
    ]
}

这里我使用两种类型的代码。 在代码A中,我使用嵌套查询,但我会得到按会话分组的输出。 在代码B中,我没有使用嵌套查询(通过使用连接避免嵌套查询)但我没有按会话分组输出。

我需要的是我不想使用嵌套查询,但我希望我的输出按会话分组。 我怎么能做到这一点?

1 个答案:

答案 0 :(得分:2)

您需要创建具有关系的模型

https://laravel.com/docs/5.6/eloquent

https://laravel.com/docs/5.6/eloquent-relationships

创建具有以下关系的GameSession模型:

function games() {
    return $this->hasMany(Game::class, 'GamesSessionId', 'Id');
}

Game模型上创建反向关系:

function session () {
    return $this->belongsTo(GameSession::class)
}

这样您就可以获得GameSession的{​​{1}}

第一种方法是急切加载

https://laravel.com/docs/5.6/eloquent-relationships#eager-loading

Game

这将为$gameSessionWithGames = GameSession::with('games')->find($gameSessionId); 提供所有GameSession s作为集合的属性

或直接通过关系:

Game

这将返回属于$gamesForSession = GameSession::find(gameSessionId)->games;

的所有Game

-

以上两者都会返回一个Collection,然后您可以GameSession

https://laravel.com/docs/5.6/collections#method-map

只获得您需要的结果

或者向查询构建器添加map

https://laravel.com/docs/5.6/queries#selects