简体   繁体   中英

Laravel dingo/api custom transformer

I am trying to implement a custom transformer using dingo api ( https://github.com/dingo/api/wiki/Transformers#custom-transformation-layer ) for my Post model and I am getting this exception:

Missing argument 2 for PostTransformer::transform(), called in /home/.../vendor/league/fractal/src/Scope.php on line 298 and defined

My controller:

$post = Post::findOrFail(2);

return $this->item($post, new PostTransformer);

My PostTransformer class:

<?php

use Illuminate\Http\Request;
use Dingo\Api\Transformer\Binding;
use Dingo\Api\Transformer\TransformerInterface;

class PostTransformer implements TransformerInterface
{
    public function transform($response, $transformer, Binding $binding, Request $request)
    {
        // Make a call to your transformation layer to transformer the given response.

        return [
            'kkk' => 'val'
        ];

    }
}

What is wrong?

Your PostTransformer isn't a Transformer. What you specified there is an TransformerLayer ( https://github.com/dingo/api/wiki/Transformers#custom-transformation-layer ).

However a Transformer in Dingo looks like this:

<?php

use League\Fractal\TransformerAbstract;

class PostTransformer extends TransformerAbstract
{
    public function transform(Post $post) {
        return [
            'id' => $post->id
            // ...
        ];
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM