2016-07-02 121 views
0

要初始化我的应用程序,我有以下途径:Laravel Dingo API - 如何应对多个收藏品/变形金刚?

/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); 
]; 

回答

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

这应该在你正在寻找的格式返回JSON。

+0

这不会起作用,因为'$ this-> response-> collection(...'将包含正常的响应信息,例如'{headers,exceptions}' – AndrewMcLagan

+0

什么样的头文件和异常?对于每个资源或那些 – Ruffles

+0

哦,我看到了,不应该用每个类的分形对象替换'$ this-> response-> collection()': '$ this-> fractal-> collection ($ taxonomies,new TaxonomyTransformer)' 所以你可以得到那里的值,并且每个类都有一个响应对象而不是一个? – Ruffles

0

我有同样的问题,我从How to use Transformer in one to many relationship. #1054找到解决方案。 这里是我想要返回与我的控制器中的流浪者的流浪者的集合。

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

在我的控制器

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

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

而且我得到的结果

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

请留意$ defaultIncludes并在UserTransformer.You includeXXX()methonds的使用可以从Fractal Doc获得更详细的信息的。

+1

尽管始终欢迎链接到潜在解决方案,但堆栈溢出的答案应始终包含答案主体本身中解决方案的主要部分/点。仅仅链接到没有进一步评论的解决方案并不被认为是可以接受的答案。参见[问]如何制定正确的答案。 – Magisch