2016-07-02 11 views
0

は、私は次のルートを持っている私のアプリを初期化します。これにより、複数のHTTP要求が保存されます。Laravel Dingo API - 複数のコレクション/トランスに対応するにはどうすればいいですか?</p> <p><code>/initialize</code></p> <p>これはタクソノミを返し、Enumerablesやコレクションなどの他の分類のカップル:

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(...'は '{ヘッダ、例外} 'のような通常の応答情報を含みます) – AndrewMcLagan

+0

どのような種類のヘッダと例外ですか? – Ruffles

+0

ああ、私は '$ this-> response-> collection()'を各クラスのフラクタルオブジェクトで置き換えてはいけません: '$ this-> fractal-> collection ($ taxonomies、new TaxonomyTransformer) ' クラスごとに1つではなく1つの応答オブジェクトを持つことができますか? – 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()); 

そして、私はFractal Docからより多くの詳細情報を取得することができます$ defaultIncludesとUserTransformer.YouでincludeXXX()methondsの利用

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

てくださいノートの結果を得ました。

+1

潜在的なソリューションへのリンクは常に歓迎しますが、スタックオーバーフローに関する答えは、解答本体そのものに常にソリューションの主要な部分/ポイントを含める必要があります。単に解説のない解へのリンクは、受け入れ可能な答えとはみなされません。適切な答えを策定する方法については、[ask]を参照してください。 – Magisch

関連する問題

 関連する問題