在 Laravel5 Fractal 中使用 parseincludes

Using parseincludes in Laravel5 Fractal

https://github.com/thephpleague/fractal 中使用 parseIncludes 挣扎。

我有两个表,属性 和 Weeks。每个 属性 有很多周。使用 Fractal,我可以 return 我的 属性 项目与周的集合。我想做的是使用 parseIncludes,这样周的 return 是可选的。

PropertyTransformer.php

<?php
namespace App\Transformer;

use App\Models\Property;
use League\Fractal\TransformerAbstract;


class PropertyTransformer extends TransformerAbstract
{

    protected $availableIncludes = [
        'week'
    ];

    public function transform(Property $property)
    {
        return [
            'id'                => (int) $property['PropertyID'],
            'PropertyName'      => $property['PropertyName'],
            'ExactBeds'         => (int) $property['ExactBeds'],
            'weeks'             => $property->week
        ];
    }

    /**
     * Include Week
     *
     * @return League\Fractal\ItemResource
     */
    public function includeWeek( Property $property )
    {
        $week = $property->week;

        return $this->item($week, new WeekTransformer);
    }
}

WeekTransformer.php

<?php
namespace App\Transformer;

use App\Models\Week;
use League\Fractal;

class WeekTransformer extends Fractal\TransformerAbstract
{


    public function transform(Week $week)
    {
        return [
            'Week'          => $week['week'],
            'Available'     => $week['available'],
            'Price'         => (int) $week['price'],

        ];
    }

}

我的PropertyController.php

<?php namespace App\Http\Controllers\Api\v1;

use App\Http\Requests;
use App\Models\Week;
use Illuminate\Support\Facades\Response;

use App\Models\Property;

use League\Fractal;
use League\Fractal\Manager;
use League\Fractal\Resource\Collection as Collection;
use League\Fractal\Resource\Item as Item;

use App\Transformer\PropertyTransformer;


class PropertyController extends \App\Http\Controllers\Controller {

public function show($id)
{
    $property = Property::with('bedroom')->with('week')->find($id);

    $fractal = new Fractal\Manager();

    if (isset($_GET['include'])) {
        $fractal->parseIncludes($_GET['include']);
    }

    $resource = new Fractal\Resource\Item($property, new PropertyTransformer);
    //$resource = new Fractal\Resource\Collection($properies, new PropertyTransformer);

    return $fractal->createData( $resource )->parseIncludes('weeks')->toJson();

}

我在 parseIncludes 上收到以下错误:-

在 class 中找不到方法 'parseIncludes' \League\Fractal\Scope

我正在按照这里关于变形金刚的指南 - http://fractal.thephpleague.com/transformers/

我想我在这里的某处出错了:-

这些包含将可用,但除非调用 Manager::parseIncludes() 方法,否则永远不会被请求:

<?php
use League\Fractal;

$fractal = new Fractal\Manager();

if (isset($_GET['include'])) {
    $fractal->parseIncludes($_GET['include']);
}

如果我删除 parseIncludes,我不会收到错误,我也会得到我的 属性 数据以及我收集的周数,但是 ?include=week 无法选择性地获取它。

您的问题出在这一行:
return $fractal->createData( $resource )->parseIncludes('weeks')->toJson();

createData() returns \League\Fractal\Scope 而且它没有 parseInlcudes 方法。

您已经在此处致电 parseIncludes

if (isset($_GET['include'])) {
    $fractal->parseIncludes($_GET['include']);
}

所以只需删除 return 语句中对它的第二次调用:
return $fractal->createData($resource)->toJson();