Laravel Dingo API - 如何回应多个集合/变换器?

To initialize my app I have the following route:

/initialize

This returns Taxonomies, Enumerables and a couple of other taxonomy like collections. This saves multiple HTTP requests.

Although with Dingo / Fractal, I cannot see how I can respond with multiple collections?

e.g.

return [
    'taxonomies' => $this->response->collection($taxonomies, new TaxonomyTransformer);
    'enumerables' => $this->response->collection($enumerables, new EnumerableTransformer);
    'otherStuff' => $this->response->collection($otherStuff, new OtherStuffTransformer);
];  
return response()->json([
    'data' =>  [
        'taxonomies' => $this->fractal->collection($taxonomies, new TaxonomyTransformer);
        'enumerables' => $this->fractal->collection($enumerables, new EnumerableTransformer);
        'otherStuff' => $this->fractal->collection($otherStuff, new OtherStuffTransformer);
    ] 
], 200);

This should return the JSON in the format you are looking for.

I have the same issue ,and I found the solution from How to use Transformer in one to many relationship. #1054. Here is the collection I want to return with the transfomer of dingo in my controller.

$user = User::where('email','=',$input['email'])->with('departments')->with('roles')->get();

DepartmentTransformer

class DepartmentTransformer extends TransformerAbstract
{
    public function transform($department)
    {
        return [
            'id' => $department['id'],
            'name' => $department['name'],
            'level' => $department['level'],
            'parent_id' => $department['parent_id']
        ];
    }
}

RolesTransformer

class RolesTransformer extends TransformerAbstract
{
    public function transform($role)
    {
        return [
            'name' => $role['name'],
            'slug' => $role['slug'],
            'description' => $role['description'],
            'level' => $role['level']
        ];
    }

}

UserTransformer

class UserTransformer extends TransformerAbstract
{
    protected $defaultIncludes = ['departments','roles'];


    public function transform($user)
    {
        return [
            'id' => $user['id'],
            'name' => $user['name'],
            'email' => $user['email'],
            'phone' => $user['phone'],
        ];
    }


    public function includeDepartments(User $user)
    {
        $dept  = $user->departments;

        return $this->collection($dept, new DepartmentTransformer());
    }


    public function includeRoles(User $user)
    {
        $rl = $user->roles;

        return $this->collection($rl, new RolesTransformer());
    }
}

In my controller

$user = User::where('email','=',$input['email'])->with('departments')->with('roles')->get();

return $this->response->collection($user, new UserTransformer());

And I got the result

            "data": {
            {
                "id": 43,
              "name": "test7",
              "email": "test7@foxmail.com",
              "phone": "186********",
              "departments": {
                "data": {
                  {
                      "id": 1,
                    "name": "业务一部",
                    "level": 1,
                    "parent_id": 0
                  }
                }
              },
              "roles": {
                "data": {
                  {
                      "name": "agent",
                    "slug": "agent",
                    "description": "业务员",
                    "level": 1
                  }
                }
              }
            }
          }

Please take note of the usage of $defaultIncludes and includeXXX() methonds in the UserTransformer.You can get more detail info from Fractal Doc.