1

This is my first Dingo Api implementation. This is my

RoleTransformer class

namespace App\Api\V1\Transformers;

use League\Fractal\TransformerAbstract;
use App\Role;

class RoleTransformer extends TransformerAbstract
{

    public function transform(Role $role)
    {

        return [
            'role_id' => (int)$role->id,
            'name'    => $role->name
        ];
    }

}

This is RoleController

namespace App\Http\Controllers;

use Dingo\Api\Routing\Helpers;
use Illuminate\Http\Request;
use Dingo\Api\Http\Response;
use App\Role;
use App\Http\Requests;
use App\Api\V1\Transformers\RoleTransformer;

class RoleController extends Controller
{
    use Helpers;

    public function index(){
        $roles = Role::all();

        return $this->response->collection($roles, new RoleTransformer())
                              ->setStatusCode(200);
    }
}

Though I was supposed to get only role_id and name

[

    {
        "id": 1,
        "name": "admin",
        "display_name": "Admin",
        "description": "Administrator",
        "created_at": "2016-07-18 08:11:44",
        "updated_at": "2016-07-18 08:11:44"
    },
    {
        "id": 2,
        "name": "director",
        "display_name": "Director",
        "description": "Country Manager",
        "created_at": "2016-07-18 08:11:44",
        "updated_at": "2016-07-18 08:11:44"
    },
    {
        "id": 3,
        "name": "manager",
        "display_name": "Manager",
        "description": "Team Manager",
        "created_at": "2016-07-18 08:11:44",
        "updated_at": "2016-07-18 08:11:44"
    }

]

Not sure what I am doing wrong.

Using Laravel 5.2 and Dingo 1.0.x@dev.

halfer
  • 19,824
  • 17
  • 99
  • 186
Ariful Haque
  • 3,662
  • 5
  • 37
  • 59
  • Found the solution. In `config/api.php`, url prefix was set as `'prefix' => env('API_PREFIX', 'api');` but in my routes, I was testing without `api` prefix. As soon I set `api` in route, its working like a charm. – Ariful Haque Jul 20 '16 at 08:34

2 Answers2

0

Isn't this part should be:

public function index(){
    $roles = Role::all();
    $rt = new RoleTransformer();

    return $this->response->collection($roles, $rt->transform($roles))
                          ->setStatusCode(200);
}

Or you can make a constructor to output only the id and name?? Let me know if it helps!

jsdecena
  • 572
  • 2
  • 7
  • 22
  • sorry but this solution is not working. `transform()` return array and 2nd parameter of `collection()` need to be a Transformer class. The code I submitted is full copy from dingo documentation.. – Ariful Haque Jul 20 '16 at 08:24
0

The same problem, but i have the prefix 'api', not working still. when debug with the code, the response has been handled as i request, after the code following, It's restored!

 //vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php
public function handle($request)
{
.
.
.
    $this->app['events']->dispatch(
            new Events\RequestHandled($request, $response)
        );

        return $response;
}

laravel 5.8.38 , dingo 2.4.7