Api: Multiple Transformers

Created on 18 Jul 2016  路  3Comments  路  Source: dingo/api

I'm creating a base controller to include some data I need on every page load and was wondering if I could create a response with multiple transformers. I pulled this solution from a stack over flow question on the same issue. The issue I am seeing is, I don't thin it actually works cause I'm not seeing any indiction that the transformer is being called.

Here is code I have in my BaseController

```

namespace App\Http\Controllers;

use \App;
use Illuminate\Http\Response;
use Dingo\Api\RoutingHelpers;
use App\Categories;
use App\Transformers\CategoriesTransformer;

class BaseController extends Controller {
use Helpers;

public function get(){
    $_categories = new Categories();
    $_categories = $_categories->with('children')->whereNull('parent')->orderBy('name', 'asc')->get();

    return $this->response->array([
        'data' =>  [
            'categories' => (!empty($_categories) ? $this->response->collection($_categories, new CategoriesTransformer) : NULL)
        ]
    ], 200);
}

}

The response I get is: 

{
"data": {
"categories": {
"exception": null,
"original": [
{
"id": 1,
"parent": null,
"name": "tools",
"description": "toolbooks",
"image": null,
"slug": "tools",
"created_at": "2016-07-16 04:15:36",
"updated_at": "2016-07-18 13:01:20",
"children": [
{
"id": 2,
"parent": 1,
"name": "Modern",
"description": "Modern tools",
"image": null,
"slug": "modern",
"created_at": "2016-07-18 12:57:56",
"updated_at": "2016-07-18 12:57:56"
}
]
}
],
"headers": {

  }
}

}
}
```

The part I can figure out is the part of the json exception: null and original: I don't know where they are coming from.

If I do a simple
return $this->response->collection($_categories, new CategoriesTransformer);

I get a correct response from the looks of it:

{ "data": [ { "id": 1, "parent": 0, "name": "tools", "description": "toolsbooks", "image": null, "slug": "tools", "children": [ { "id": 2, "parent": 1, "name": "Modern", "description": "Modern tools", "image": null, "slug": "modern", "created_at": "2016-07-18 12:57:56", "updated_at": "2016-07-18 12:57:56" } ] } ] }

What am I missing here, is it even possible to do a return of multiple collections? Is that stackover flow answer correct? Any advice or help would be helpful.

discussion

Most helpful comment

So to do something like this @Kracheads and @andrewmclagan you'd be better served accessing the fractal manager directly instead of utilizing the methods and helpers build into the package.

So for example you're controller would look something like this:

namespace App\Http\Controllers;

use \App;
use Illuminate\Http\Response;
use Dingo\Api\Routing\Helpers;
use App\Categories;
use App\Transformers\CategoriesTransformer;
use League\Fractal\Manager as FractalManager;
use League\Fractal\Resource\Collection;

class BaseController extends Controller {
    use Helpers;

    public function __construct(FractalManager $fractal) {
        $this->fractal = $fractal;
    }

    public function get(){
        $_categories = new Categories();
        $_categories = $_categories->with('children')->whereNull('parent')->orderBy('name', 'asc')->get();

        return $this->response->array([
            'data' =>  [
                'categories' => (!empty($_categories) ? $this->fractal->createData(new Collection($_categories, new CategoriesTransformer))->toArray() : NULL)
            ]
        ], 200);
    }
}

All 3 comments

So to do something like this @Kracheads and @andrewmclagan you'd be better served accessing the fractal manager directly instead of utilizing the methods and helpers build into the package.

So for example you're controller would look something like this:

namespace App\Http\Controllers;

use \App;
use Illuminate\Http\Response;
use Dingo\Api\Routing\Helpers;
use App\Categories;
use App\Transformers\CategoriesTransformer;
use League\Fractal\Manager as FractalManager;
use League\Fractal\Resource\Collection;

class BaseController extends Controller {
    use Helpers;

    public function __construct(FractalManager $fractal) {
        $this->fractal = $fractal;
    }

    public function get(){
        $_categories = new Categories();
        $_categories = $_categories->with('children')->whereNull('parent')->orderBy('name', 'asc')->get();

        return $this->response->array([
            'data' =>  [
                'categories' => (!empty($_categories) ? $this->fractal->createData(new Collection($_categories, new CategoriesTransformer))->toArray() : NULL)
            ]
        ], 200);
    }
}

@hskrasek

This is incorrect in the fact it does not run the serializers.

Was this page helpful?
0 / 5 - 0 ratings