I've got the API working using the standard process, but I want to remove the data
namespace from the JSON output. I see I need to implement ArraySerializer, I have been through the Fractal docs, but I can't work out where I need to added it in Laravel 5.2
I found this answer but I'm just getting the same output at the line of code I commented out:
class TrackController extends ApiController
{
public function index()
{
$tracks = Track::all();
//return $this->respondWithCollection($tracks, new TrackTransformer);
// Same response as the commented out line above
$response = new \League\Fractal\Resource\Collection($tracks, new TrackTransformer);
$manager = new \League\Fractal\Manager();
$manager->setSerializer(new \League\Fractal\Serializer\ArraySerializer());
return response()->json($manager->createData($response)->toArray());
}
public function show($id)
{
$track = Track::find($id);
return $this->respondWithItem($track, new TrackTransformer);
}
}
Also, I'm implementing this on a specific controller, even if I got this working, where do I add the code/class so I can get ArraySerializer output for all my controllers?
I've posted this on Github if that helps.
$response = new \League\Fractal\Resource\Collection($tracks, new TrackTransformer);
after you set serializer – Macaluso$manager = new \League\Fractal\Manager(); $manager->setSerializer(new \League\Fractal\Serializer\ArraySerializer()); $response = new \League\Fractal\Resource\Collection($tracks, new TrackTransformer);
– Macalusodata
. – Cesta$response = new \League\Fractal\Resource\Collection($tracks, new TrackTransformer, 'MYKEY')
without specify a key, fractal will automatically fall back to use thedata
key – MacalusoCyvelnet/laravel5-fractal
may save you some times in making data tranformation – Macaluso{ "message": "The Response content must be a string or object implementing __toString(), \"object\" given.", "status_code": 500 }
and this is my codereturn new \League\Fractal\Resource\Collection($request->user->friends, new FriendTransformer(), 'friends');
– Cuffs$manager->createData($response)->toArray()
– Macalusodata
key insteadfriends
, here's my new code$resource = (new \League\Fractal\Resource\Collection($request->user->friends, new FriendTransformer(), 'friends')); $manager = new Manager(); return $manager->createData($resource)->toArray();
can you tell me what I'm doing wrong? – Cuffs$manager->setSerializer(new \League\Fractal\Serializer\ArraySerializer());
before you instantial fractal manager, because fractal uses DataArraySerializer which will uses data namespace – Macaluso$this->response->collection($collection, new Transformer, 'key')
? Thanks – Cuffs