php - Laravel + 分形 "deeply"嵌套包含

标签 php web-services laravel-5

我正在使用分形 ( fractal.thephpleague.com ) 通过 Laravel ( laravel.com ) 开发 API。顺便说一句,这是一个了不起的图书馆。

在某些网络服务中,我需要返回几个嵌套模型的信息,这些模型有 3 层深。也就是说,我有一个 Survey 模型,它有很多 Survey Items,并且每个模型都有许多 Survey Item Results(每个用户)。好吧,我需要他们所有的数据,分类,即:

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

使用转换器和包含,我可以毫无问题地获得调查和调查项目信息,但我还需要调查项目结果... 也就是说,我需要类似2-level“嵌套”的东西来获取third 级别的信息。

到目前为止我最好的方法(只返回两个级别:调查和调查项目)。在我的 Controller 中:

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

非常感谢任何帮助。 提前致谢。

最佳答案

这是我通常做的事情

调查转换器

<?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;
    }
}

在我的基本 Controller 上

    /**
     * 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'])

关于php - Laravel + 分形 "deeply"嵌套包含,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42544841/

相关文章:

安卓远程数据库

php - Laravel 5 - ORM has() 关系(反向 - notHas)

laravel - 如何从 Laravel 5 中的两个相关表中获取数据?

php - SQL查询只返回第一行

php - Laravel 没有将模型加载到路由中

python - 无法编码 <type 'datetime.date' > 对象

web-services - 使用 Reporting Services Web 服务,如何获得特定用户的权限?

javascript - Laravel - Blade 附加部分

php - 服务器如何存储使用 PHP 创建的 session 变量?

php - 无法从 php 中的 $_FILES 获取多个上传的文件