Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set custom serializer for includes file #392

Open
stefensuhat opened this issue Jun 21, 2017 · 6 comments
Open

Set custom serializer for includes file #392

stefensuhat opened this issue Jun 21, 2017 · 6 comments
Labels

Comments

@stefensuhat
Copy link

HI,

I've been wondering is there any chance that i can use above class at includes?

For example:


class OrderTransformer extends TransformerAbstract
{
    public $availableIncludes = ['user', 'items'];

    /**
     * A Fractal transformer.
     *
     * @param $resource
     * @return array
     */
    public function transform($resource)
    {
        return [
            'id'         => $resource->id,
            'receiver'   => $resource->receiver,
            'totalPrice' => $resource->totalPrice,
            'address'    => $resource->address,
            'phone'      => $resource->phone,
            'orderedAt'  => $resource->createdAt->format('Y-m-d H:i:s'),
        ];
    }

    public function includeUser($resource)
    {
        $resource = $resource->user;

        return $this->item($resource, new UserTransformer);
    }

    public function includeItems($resource)
    {
        $resource = $resource->items;

        return $this->collection($resource, new OrderItemTransformer); // I want to use custom serializer here to remove "data" key.
    }
}

I need to use those fractal to remove the additional data key on the result.

Here is the result I get:

{
    "orders": [
        {
            "id": "C170620PVJM6",
            "receiver": "Example",
            "totalPrice": 10,
            "address": "Anywhere near the horizon",
            "phone": "081234567",
            "orderedAt": "2017-06-20 22:42:43",
            "items": {
                "data": [
                    {
                        "price": 2,
                        "menu": {
                            "name": "VERY tired of.",
                            "image": {
                                "web": "https://lorempixel.com/413/413/?87962",
                                "mobile": "https://lorempixel.com/413/413/?87962",
                                "square": "https://lorempixel.com/413/413/?87962"
                            }
                        }
                    },
                    {
                        "price": 6,
                        "menu": {
                            "name": "Queen jumped up in.",
                            "image": {
                                "web": "https://lorempixel.com/413/413/?77046",
                                "mobile": "https://lorempixel.com/413/413/?77046",
                                "square": "https://lorempixel.com/413/413/?77046"
                            }
                        }
                    },
                    {
                        "price": 2,
                        "menu": {
                            "name": "And oh, I wish you.",
                            "image": {
                                "web": "https://lorempixel.com/413/413/?22822",
                                "mobile": "https://lorempixel.com/413/413/?22822",
                                "square": "https://lorempixel.com/413/413/?22822"
                            }
                        }
                    }
                ]
            }
        }
    ],
    "meta": {
        "pagination": {
            "total": 5,
            "count": 1,
            "per_page": 1,
            "current_page": 1,
            "total_pages": 5,
            "links": {
                "next": "https://api.butterfish.dev/v1/user/orders?page=2"
            }
        }
    }
}

Expected result:

{
    "orders": [
        {
            "id": "C170620PVJM6",
            "receiver": "Example",
            "totalPrice": 10,
            "address": "Anywhere near the horizon",
            "phone": "0123123123",
            "orderedAt": "2017-06-20 22:42:43",
            "items": [
                    {
                        "price": 2,
                        "menu": {
                            "name": "VERY tired of.",
                            "image": {
                                "web": "https://lorempixel.com/413/413/?87962",
                                "mobile": "https://lorempixel.com/413/413/?87962",
                                "square": "https://lorempixel.com/413/413/?87962"
                            }
                        }
                    },
                    {
                        "price": 6,
                        "menu": {
                            "name": "Queen jumped up in.",
                            "image": {
                                "web": "https://lorempixel.com/413/413/?77046",
                                "mobile": "https://lorempixel.com/413/413/?77046",
                                "square": "https://lorempixel.com/413/413/?77046"
                            }
                        }
                    },
                    {
                        "price": 2,
                        "menu": {
                            "name": "And oh, I wish you.",
                            "image": {
                                "web": "https://lorempixel.com/413/413/?22822",
                                "mobile": "https://lorempixel.com/413/413/?22822",
                                "square": "https://lorempixel.com/413/413/?22822"
                            }
                        }
                    }
                ]
        }
    ],
    "meta": {
        "pagination": {
            "total": 5,
            "count": 1,
            "per_page": 1,
            "current_page": 1,
            "total_pages": 5,
            "links": {
                "next": "https://api.butterfish.dev/v1/user/orders?page=2"
            }
        }
    }
}

Can I include custom serializer on that includes?

@DeRain
Copy link

DeRain commented Jul 17, 2017

Faced the same issue. It would be great to get any feedback from package maintainers.

@maxim-usikov
Copy link

@DeRain @stefensuhat
i think that's why:

Collections keep the 'data' namespace to avoid confusing JSON when meta data is added.

See here: http://fractal.thephpleague.com/serializers/

@JayBizzle
Copy link

See #315 (comment) for a work around

@mcblum
Copy link

mcblum commented Mar 12, 2018

@JayBizzle is there any reason why this workaround would no longer be working? Our app was working great for the longest time, then we updated to Laravel 5.5 and all of a sudden I can't get that data key to go away. I've tried setting the default_serializer in the config file and using the manager to set a custom serializer.

@mouadhbenalaya
Copy link

You must change the default serializer in config/fractal.php
default_serializer' => 'League\Fractal\Serializer\ArraySerializer',

@stale
Copy link

stale bot commented Apr 16, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 4 weeks if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Apr 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants