0

要初始化我的应用程序,我有以下路线:

/initialize

这将返回分类、枚举和其他一些分类,如集合。这样可以节省多个 HTTP 请求。

尽管使用 Dingo / Fractal,但我看不到如何使用多个集合来响应?

例如

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

2 回答 2

1
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.

于 2016-07-03T19:23:00.907 回答
0

我有同样的问题,我找到了如何在一对多关系中使用 Transformer 的解决方案。第1054章 这是我想在我的控制器中使用 dingo 变压器返回的集合。

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

部门变压器

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

角色Transformer

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

}

用户变压器

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());
    }
}

在我的控制器中

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

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

我得到了结果

            "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
                  }
                }
              }
            }
          }

请注意 UserTransformer 中 $defaultIncludes 和 includeXXX() 方法的使用。您可以从Fractal Doc获取更多详细信息。

于 2016-11-28T12:05:30.097 回答