My API works using a standard process, but I want to remove the data
namespace from the JSON output. I see that I need to implement ArraySerializer, I went through Fractal docs, but I cannot decide where I need to add it in Laravel 5.2
I found this answer , but I just get the same output in the line of code that I commented:
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 implement this on a specific controller, even if I got this working, where can I add code / class so that I can get ArraySerializer output for all my controllers?
I posted this on Github if that helps.
source share