PHP fractal transformer returns embedded data

I am trying to use Fractal to convert API data output. This works for individual elements and collections, but I cannot get it to work with inline data. Unfortunately, I cannot find a lot of β€œhow's” on Fractal. I followed the information on the Fractal website, but this will not work. I am using Laravel 4 as a frame.

This is what I have in my Transformer class:

    protected $availableEmbeds = array(
    'requirements'
);
    public function transform(){ etc... }

    public function embedRequirements(Regions $regions)
{
    return $this->collection($regions->requirements, new RequirementsTransformer);
}

Than inside my controller I have

    $regions = Regions::with($this->eagerLoad)->get();

This gives me the result I want.

But when I transfer this data to the transformer, it does not give the desired result:

    return $this->respondWithCollection($regions, new RegionTransformer());

RespondWithCollection Method

    protected function respondWithCollection($collection, $callback)
{
    $resource = new Collection($collection, $callback);

    $fractalManager = new Manager();
    $rootScope = $fractalManager->createData($resource);
    //$rootScope = $this->fractal->createData($resource);

    return $this->respondWithArray($rootScope->toArray());
}

This is the conclusion:

    {
"data": [
    {
        "id": 36218,
        "name": "Netherlands",
        "active": true,
        "created": "2014-02-28 11:17:02"
    }
],
"embeds": [
    "requirements"
]

}

Where I expected the "requirements" to be part of the relationship key inside the "data" key.

Does anyone know what I'm doing wrong?

+4
1

.

:

public function embedRequirements(Regions $regions)
{
    $requirements = $regions->requirements()->get();
    return $this->collection($requirements, new RequirementsTransformer);
}

get()

$regions- > () β†’ ()

+1

Source: https://habr.com/ru/post/1539013/


All Articles