2017-03-02 124 views
2

我正在使用分形(fractal.thephpleague.com)与Laravel(laravel.com)开发API。顺便说一下,这是一个了不起的图书馆。Laravel +分形“深”嵌套包括

在某些Web服务中,我需要返回几个嵌套模型的信息,这些嵌套模型有3个深度。也就是说,我有一个调查模型,其中有许多调查项目,并且他们每个人都有很多调查项目结果(每个用户)。好吧,我需要从所有这些数据,分类,那就是:

"surveys": [ 
    { 
     "id": 1, 
     ..., 
     "items": [ 
      { 
       "id": 14, 
       ..., 
       "results": [ 
        { 
         "id": 45, 
         ... 
        }, 
        { 
         ... 
        } 
       ] 
      }, 
      { 
       ... 
      } 
     ] 
    }, 
    { 
     ... 
    } 
} 

随着变压器和包括我得到的调查和调查项目的信息没有问题,但我还需要调查项目结果... 也就是我需要的东西类似2级“嵌套”包括,以获得第三级别的信息。

到目前为止,我的最佳做法(只返回两个级别:调查和调查项目)。在我的控制器中:

return fractal() -> transform(
    Survey::where(...), 
    new SurveyTransformer() 
) -> include(['SurveyItems']) -> respond(); 

任何帮助,非常感谢。 在此先感谢。

+0

你有包括您ItemTransformer结果吗? –

+0

当然可以。 – andcl

回答

2

这就是我通常做

调查变压器

<?php 
namespace App\Transformers; 

use League\Fractal; 
use App\Survey; 

class SurveyTransformer extends Fractal\TransformerAbstract 
{ 
    /** 
    * List of resources possible to include 
    * 
    * @var array 
    */ 
    protected $availableIncludes = [ 
     'items' 
    ]; 

    public function transform(Survey $survey) 
    { 
     return [ 
      'id' => (int) $user->id, 
     ]; 
    } 


    /** 
    * Include Items 
    * 
    * @param App\Survey $survey 
    * @return League\Fractal\CollectionResource 
    */ 
    public function includeItems(Survey $survey) 
    { 
     $items = $survey->items; 
     if (!is_null($items)) { 
      return $this->collection($items, new ItemTransformer); 
     } 
     return; 
    } 
} 

项目变压器

<?php 
namespace App\Transformers; 

use League\Fractal; 
use App\Item; 

class ItemTransformer extends Fractal\TransformerAbstract 
{ 
    /** 
    * List of resources possible to include 
    * 
    * @var array 
    */ 
    protected $availableIncludes = [ 
     'results' 
    ]; 

    public function transform(Item $item) 
    { 
     return [ 
      'id' => (int) $user->id, 
     ]; 
    } 


    /** 
    * Include results 
    * 
    * @param App\Item $item 
    * @return League\Fractal\CollectionResource 
    */ 
    public function includeResults(Item $item) 
    { 
     $results = $item->results; 
     if (!is_null($results)) { 
      return $this->collection($results, new ResultTransformer); 
     } 
     return; 
    } 
} 

在我的基本控制器

/** 
    * get fractal tranformed data 
    * @param $resource 
    */ 
    protected function fractalResponse($resource, array $includes = []) 
    { 
     $manager = new Manager(); 
     $manager->setSerializer(new DataArraySerializer()); //or what ever you like 

     if (sizeof($includes) == 0) { 
      $data = $manager->createData($resource) 
       ->toArray(); 
     } else { 
      $manager->parseIncludes($includes); 
      $data = $manager->createData($resource) 
       ->toArray(); 
     } 
     return $data; 
    } 

然后

$resource = new \League\Fractal\Resource\Collection($survies, new SurveyTransformer); 
$response_data = $this->fractalResponse($resource, ['items.results']) 
+0

感谢您的回复,但我已经完全编写了该结构(SurveyTransformer中的includeItems和SurveyItemTransformer中的includeResults),并且它返回的项目及其末尾都有一个名为“items.results”的键,其值为'null'。任何可能导致此行为的想法? – andcl

+0

在itemtransformer上的includeResuts方法上添加转储时看到结果吗? –

+0

好的,我在API中使用了Laravel Responder(https://github.com/flugger/laravel-responder)软件包,看起来这个功能在软件包中是有问题的(https://github.com/flugger/laravel-responder/issues/33)...所以我要改成另一个Laravel-fractal软件包或试着解决这个问题,因为到目前为止我已经开发了很多东西来改变一切...无论如何,非常感谢你。 – andcl