Trying to delete data in Fractal by running ArraySerializer in Laravel 5.2

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.

+5
source share
2 answers

You need to create your own MySerializer extends DataArraySerializer and use it instead of the default value. By default, DataArraySerializer has three methods, and in each of them you need to change the return values ​​to this:

 use League\Fractal\Serializer\DataArraySerializer; class MySerializer extends DataArraySerializer { /** * Serialize a collection. * * @param string $resourceKey * @param array $data * * @return array */ public function collection($resourceKey, array $data) { return $data; } /** * Serialize an item. * * @param string $resourceKey * @param array $data * * @return array */ public function item($resourceKey, array $data) { return $data; } /** * Serialize null resource. * * @return array */ public function null() { return []; } } 

So, you change the behavior of the serialization mechanism and get the result as you wish.

+2
source

If you use spatie / laravel-fractal or spatie / fractalistic , you can remove the data key from the results using the Spatie Spatie\Fractalistic\ArraySerializer() array serializer instead of the default Fractal serializer.

You can use it like this:

 Fractal::create() ->collection($books) ->transformWith(function($book) { return ['id' => $book['id']];}) ->serializeWith(new \Spatie\Fractalistic\ArraySerializer()) ->toArray(); 

It will be back.

 [ ['id' => 1], ['id' => 2] ] 

Instead..

 [ 'data' => [ ['id' => 1], ['id' => 2] ] ] 

Or, if you want to use the Spatie serializer globally, add it to the config/fractal.php as follows:

 <?php return [ /* * The default serializer to be used when performing a transformation. It * may be left empty to use Fractal default one. This can either be a * string or a League\Fractal\Serializer\SerializerAbstract subclass. */ 'default_serializer' => new Spatie\Fractalistic\ArraySerializer(), . . . ]; 

Additional Information: Using the serializer

0
source

Source: https://habr.com/ru/post/1245907/


All Articles