Laravel雄辩的嵌套组的关系

时间:2018-08-24 08:14:08

标签: php laravel laravel-5 eloquent

请帮助我如何基于游戏类别关系添加另一个嵌套组。我有3个表与laravel 5.4雄辩相关。

游戏提供商表

+----+-------------+--------------+------------------+
| id | game_name   | game_type_id | game_category_id |
+----+-------------+--------------+------------------+
|  1 | Sample      |      1       |         1        |
|  1 | Sample 0    |      1       |         2        |
|  2 | Sample 1    |      2       |         1        |
|  3 | Sample 2    |      2       |         2        |
+----+-------------+--------------+------------------+

游戏类型

+----+-------------+
| id | name        |
+----+-------------+
|  1 | Chess       |
|  2 | Poker       |
+----+-------------+

游戏类别

+----+-------------+
| id | name        |
+----+-------------+
|  1 | Recommended |
|  2 | Optional    |
+----+-------------+

我已经完成了第一个嵌套的游戏类型关系的操作,但是我不知道如何为游戏类别添加另一个分组

$game_providers = GameProvider::all();

$game_providers = $game_providers->groupBy(function ($game_provider) {
    return $game_provider->game_type->name;
})->all();

及其输出

Array{
 'Chess' => Array{
    0 => Array{
     'id'               => 1,
     'game_name'        => 'Sample',
     'game_type_id'     => 1,
     'game_category_id' => 2
    },
    1 => Array{
     'id'               => 2,
     'game_name'        => 'Sample 0',
     'game_type_id'     => 1,
     'game_category_id' => 2
    },
  },
 'Poker' => Array{
    0 => Array{
     'id'               => 3,
     'game_name'        => 'Sample 1',
     'game_type_id'     => 2,
     'game_category_id' => 1
    },
    1 => Array{
     'id'               => 4,
     'game_name'        => 'Sample 2',
     'game_type_id'     => 2,
     'game_category_id' => 2
    },
  },

}

我的预期输出。

Array{
 'Chess' => Array{
    'Recommended' => Array{
        0 => Array{
           'id'               => 1,
            'game_name'        => 'Sample',
            'game_type_id'     => 1,
            'game_category_id' => 2
        }
    },
    'Optional' => Array{
        0 => Array{
            'id'               => 2,
            'game_name'        => 'Sample 0',
            'game_type_id'     => 1,
            'game_category_id' => 2
        }
    }
  },
 'Poker' => Array{
    'Recommended' => Array{
        0 => Array{
            'id'               => 3,
            'game_name'        => 'Sample 1',
            'game_type_id'     => 2,
            'game_category_id' => 1
        }
    },
    'Optional' => Array{
        0 => Array{
            'id'               => 4,
            'game_name'        => 'Sample 2',
            'game_type_id'     => 2,
            'game_category_id' => 2
        }
    }     
  }

}

2 个答案:

答案 0 :(得分:1)

应该类似于:

GameProvider::with('game_categories.game_types')->get()->map(function ($game) {
    return $game->game_categories->groupBy('name')->map(function ($catGame) {
        return $catGame->map(function ($category) {
            return $category->game_types->groupBy('name');
        });
    });
});

答案 1 :(得分:0)

使用L5,可以做到这一点的一种方法是:

GameProvider::with('game_categories.game_types')
    ->get()
    ->groupBy([
        'gameTypes.name',
        'gameCategories.name'
    ]);