2016-07-02 119 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