jadjoubran / laravel5-angular-material-starter

Get started with Laravel 5.3 and AngularJS (material)
https://laravel-angular.readme.io/
MIT License
1.66k stars 400 forks source link

Dingo Fractal Transformer not working with version 3.2 #369

Closed arifulhb closed 8 years ago

arifulhb commented 8 years ago

This is my first Dingo Api implementation. This is my

RoleTrasnformer 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. Please help me to identify the glitch.

I've searched over the stackoverflow and other forums and it seems Dingo has issue with Laravel 5.2 but didn't get anything solid or how to solve it.

arifulhb commented 8 years ago

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.