vendredi 5 octobre 2018

Add Transformer into meta: Laravel API

So, I have this code

public function show($store_slug, $taxonomy_slug)
    {
        $this->authorizeApiUser('taxonomy.show-all');

    $taxonomy = Taxonomy::where('slug', $taxonomy_slug)
                ->firstOrFail();

    $related    = Taxonomy::where('parent_id', $taxonomy->uuid)->get();

    return $this->response
                ->item($taxonomy, new TaxonomyTransformer)
                ->addMeta('related', $related, new TaxonomyTransformer); //doesn't work

}

and I have a TaxononomyTransformer

class TaxonomyTransformer extends TransformerAbstract
{
    public function transform(Taxonomy $taxonomy)
    {
        // Retrieve list of media items
        $media = [];
        foreach ($taxonomy->media as $media_item) {
            $url = $media_item->getUrl();
            $media_item->url = $url;
            $media[] = $media_item->only([
                'uuid',
                'collection_name',
                'name',
                'file_name',
                'mime_type',
                'url'
            ]);
        }

        // Finalize content
        return [
            'uuid'              => $taxonomy->uuid,
            'slug'              => $taxonomy->slug,
            'parent'            => $taxonomy->parent,
            'type'              => $taxonomy->type,
            'name'              => $taxonomy->name,
            'description'       => $taxonomy->description,
            'short_description' => $taxonomy->short_description,
            'media'             => $media,
            'created_at'        => $taxonomy->created_at->toIso8601String(),
            'updated_at'        => $taxonomy->updated_at->toIso8601String()
        ];
    }
}

that returns the media of an object. The response I get is:

"related": [
            {
                "uuid": "2de60820-c86c-11e8-acd2-a3d51d06f038",
                "store_id": null,
                "parent_id": "00a2c5af-a04f-11e8-8140-8a8552a22d72",
                "slug": "sample",
                "name": "sample",
                "type": "brand",
                "description": "<p>henlo</p>",
                "short_description": "<p>henlo</p>",
                "created_at": "2018-10-05 06:59:18",
                "updated_at": "2018-10-05 06:59:18"
            },
            {
                "uuid": "b2ff89e0-c86e-11e8-a430-313e82e4b4d0",
                "store_id": null,
                "parent_id": "00a2c5af-a04f-11e8-8140-8a8552a22d72",
                "slug": "hehehe2",
                "name": "Hehehe2",
                "type": "brand",
                "description": "<p>dfv</p>",
                "short_description": "<p>dfd</p>",
                "created_at": "2018-10-05 07:17:20",
                "updated_at": "2018-10-05 07:17:20"
            }

but it should have the "media" after the "short description" and before "created_at" and "updated_at" like this:

        "media": [
            {
                "uuid": "0a544030-c91a-11e8-8767-f19ef9efe00a",
                "collection_name": "banner-image",
                "name": "Screen Shot 2018-10-06 at 11.41.51 AM",
                "file_name": "NCU7vT2kVsfSDGj0Qhwxu5LcflDz5wzRsfY6irdt.png",
                "mime_type": "image/png",
                "url": "https://s3-ap-southeast-1.amazonaws.com/banner-image/0a544030-c91a-11e8-8767-f19ef9efe00a/NCU7vT2kVsfSDGj0Qhwxu5LcflDz5wzRsfY6irdt.png"
            }
        ],
        "created_at": "2018-07-21T15:25:20+00:00",
        "updated_at": "2018-10-06T03:43:51+00:00"

am I doing something wrong or do I have to change my approach and not add it into meta? Or Am I just forgetting something? Thank you for reading and advance Thank you for answering. It will mean a lot to me. Thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire